Common git commands

Source: Internet
Author: User
Tags git commands

The following are common commands used by git, which roughly summarize the following. Many Commands are used for Version Control Using git, but the common commands are also within the scope of the brain, share your summary with you.

1. Create a git Repository: use CD to switch to the desired directory and use "Git-init" for git initialization (-indicates space)

2. Add a file to the GIT repository by using Git-Add. The commit operation can be performed only when Git-add is called.

3. Git-RM delete files from the library

4. Git-ls-files to view the files in the current git Library

5. Check the status of the version library by using Git-status (it is recommended that you use this command to confirm the status of the database before each commit. To avoid misoperation .)

The most common misoperation: if a file is modified, the commit operation is called directly without calling Git-add to notify the GIT database that the file has changed. As a result, the file is not actually submitted. If the developer thinks that the file has been submitted and continues to modify or even delete the file, the modified content is not managed by version. If you use Git-status to view the error before each commit, you can find this error. Therefore, if you call the git-STATUS Command, pay special attention to the files that prompt "changed but not updated. These files are all changed compared with the previous commit, but they are not identified by Git-add.

Submit changes to the version library Git-commit. Directly calling Git-commit will prompt you to enter comments.

1. You can use Git-commit-M "Comments" and must have comments. Otherwise, you cannot submit the comments.

2. Git-commit also has a-a parameter that can be forcibly submitted without any changes identified by Git-Add. However, this method is not recommended.

3. For each commit, Git creates a unique commit ID code for the global code. You can use the git-revert command to restore the code at any commit.

4. You can use Git-diff to check which files have changed.

5. After submission, you can run the git-log command to view the submission record.

 

Git-branch: When Git-init is executed for the first time, the system creates a branch named "master. Other branches are created manually.

1. Create a Personal Work Branch to avoid too much interference to the master of the master branch and facilitate communication and collaboration with others.

2. When performing high-risk work, it is better to create a pilot branch and discard a mess than to clean it up.

3. view the existing branch in Git-branch

4. Git-branch name: Create Branch

Git-branch name

Git-checkout-B branch name

In the first method, although a branch is created, the current working branch is not switched to the new branch. Therefore, you also need to run the "Git-checkout branch name" command to switch, the second method not only creates a branch, but also switches the current working branch to the branch.

1. Delete the branch Git-branch-d

The git-branch-D branch name can be used to delete a branch, but be careful that after the branch is deleted, all changes that occur in the branch cannot be recovered.

2. Switch the branch Git-checkout branch name

If the Branch already exists, you can use the git-checkout branch name to switch the working branch to the branch name.

3. view the history branch Git-show-branch

 

 1 * [dev1] d2 2  3 ! [master] m2 4  5 -- 6  7 * [dev1] d2 8  9 * [dev1^] d110 11 * [dev1~2] d112 13 *+ [master] m2

 

    1. In the preceding example, the two rows above "--" indicate that there are two branches: dev1 and master, and the last commit log of the dev branch is "D2 ", the last submitted log on the master branch is m2 ". "--" Indicates the evolution history of the branch. dev1 indicates the last commit that occurred on the dev branch, dev ^ indicates the second-to-last commit on the dev branch. Dev1 ~ 2 indicates the last and third commit on the dev branch.

    2. Merge branch-Git-Merge

      Git-merge is used as the source branch of the target branch merged by Git-merge "Some memo. For example:

      If there is a conflict in the merge, git will prompt that git-merge is rarely used at present and replaced by Git-pull.

      Usage: Source branch of the target branch merged by Git-pull. For example, Git-pull. dev1

       

  1. Obtain a git-clone repository remotely

    The remote git library obtained through Git-clone only contains the current working branch of the remote git library. If you want to obtain other branch information, you need to use "Git-branch-R" to view it. If you need to obtain other remote branch code, you can run the "Git checkout-blocal branch name remote branch name" command. The remote branch name is the branch name listed in Git-branch-R, it is generally like "origin/branch name. If the local branch name already exists, the "-B" parameter is not required.

  2. Obtain a git branch from a remote location-Git-pull

    Git-pull [email protected]: Remote repository name: Remote branch name: local branch name. This command obtains a local branch of the local git library from the remote branch name of the remote git library. If the local branch name is not specified, the default pull is to the local branch. Note that git-pull can also be used to merge branches. It works the same way as Git-merge. Therefore, if your local branch already has content, Git-pull will merge these files. If there is a conflict, an alert will be reported.

  3. Submit the local branch content to the remote branch-Git-push

    Git-push and git-pull are the opposite. They submit the content of a local branch to a remote branch. Git-push [email protected]: Remote repository name local branch name: Remote branch name. This command pushes a local branch of the local git Library to the remote branch name of the remote git library.

  4. Database reversal and restoration-Git-Reset

    In addition to Resetting discarded R & D code, database reversal and restoration also play an important role. For example, we cloned a code library from a remote server and submitted it back to the remote server after local development. However, the local code library has a functional commit and a commit for backup purposes during development. In short, there are a lot of useless logs in commit logs. We do not want to submit these logs to the database when they are submitted back to the remote. Therefore, Git-Reset is required. The concept of Git-Reset is complicated. Command Format: Git-Reset [-- mixed | -- soft | -- hard] [<commit-ish>]

     

    Command Options:

    -- Mixed

    This is the default option. For example, Git-Reset [-- mixed] dev1 ^ (for the definition of dev1 ^, see 2.6.5 ). It only resets the branch status to dev1 ^, but does not change the content of any working file. That is, all file changes from dev1 ^ To dev1 are retained, but all commit logs between dev1 ^ and dev1 are cleared, and, the changed file content is not identified by Git-add. If you want to re-commit the file, you also need to perform Git-add on the changed file. In this way, a very clean submission record is obtained after the commit operation.

    -- Soft

    It is equivalent to doing Git-reset-mixed, and then done Git-add for the changed file. If this option is used, you can directly commit it.

    -- Hard

    This command will cause the rollback of all information, including the file content. It is generally used only when the discarded code is reset. After execution, the file content cannot be restored.

     

 

Common git commands

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.