Summarize your common Git commands

Source: Internet
Author: User

Git has been used for some time. You can record frequently-used commands with your own descriptions, so that you can easily prepare your own notes and help others refer to them.

Directory:

 

Basic commands:

Git clone copy and track remote master branches. The advantage of tracing is that you can directly submit or obtain the latest remote code through the pull and push commands in the future without specifying the remote branch name.

Git submodule init

Git submodule update

 

Reference

HEAD points to the current commit object and also indicates the branch on which we work. So when we use the HEAD to operate the pointer, the current commit point is not changed.

 

 

Compare this figure to understand the relationship between two sections of commit, work tree, index, and branch.

Display Information commands
Git ls-files-u displays conflicting files, and-s displays files marked as conflicting files that have been resolved

Git diff vs workspace and stage files
Git diff -- cached comparison between stage and branch

Git branch: list all branchs under the current repository
Git branch -- a lists all branch under local and remote

Git ls-files -- stage check files saved in stage

Git log displays all the commit records directed to the HEAD until the commit. Use reset HEAD ~ If the n command moves the HEAD Pointer Forward, the commit record after the HEAD is not displayed.

Git log-g queries reflog to check which actions have been performed recently, so that it can work with git branch to restore the previously discarded commit object because of moving the HEAD pointer. If reflog is lost, you can use git fsck -- full to view the unreferenced commit object.
Git log-p-2 compares the latest two commit objects
Log-1 HEAD

Git log -- pretty = oneline

Git log -- stat 1a0000e view records of the commit object whose sha1 is 1a0000e

Git blame-L 12, 22. cs if you find a method in your code has a defect, you can use git blame to mark the file and check who modified each line of the method on which day. The following example uses the-L option to limit the output range from 12th to 22.

Create a class command
Git brach branchName create branchName
Switch git checkout branchName to branchName's branch
Git checkout-B creates and switches, that is, the merge of the above two Commands

Git brach branchName ef71 create branch named branchName from commit ef71

Undo command
For a single file
1. use "git reset HEAD <file>..." to unstage
If you have added the file to stage by using the add command, you need to undo it from stage first.

And then undo it from the workspace.
2. use "git checkout -- <file>..." to discard changes in working directory

Git checkout a.txt changes to remove a.txt (files in the work zone)
Multiple files
Git chenkout.

If you already have a commit, you must
Git commit -- amend can be modified. This can only be modified last time, that is, a new commit is used to overwrite the previous commit. Therefore, it is dangerous to perform this operation after the push operation.

$ Git reset -- hard HEAD discard the workspace and index changes, and the HEAD pointer still points to the current commit. (refer to the first figure)

This command can also be used to cancel the merge without commit. In fact, the principle is to discard the index and workspace changes, because no commit Changes only exist in the index and workspace.

$ Git reset -- hard HEAD ^ is used to undo commit content (equivalent to git reset -- hard HEAD ~ 1 ). The principle is to discard the workspace and index changes, and the HEAD pointer refers to the forward commit object.

Git revert is also an undo command, the difference is that the reset points to the original or forward pointer, git revert is to create a commit to overwrite the current commit, the pointer moves backward

Submit class commands
Git add tracks changes to new or existing files or resolves conflicts

Git commit submits files from stage to branch

Git commit-a submits the modified file to stage, and then submits the file from stash to branch.

Delete class commands
Git rm -- cached readme.txt is only deleted from stage and physical files are retained.

Git rm readme.txt not only deletes physical files from stage, but also deletes physical files

Git mv a.txt B .txt rename a.txt to B .txt

Merge commands

In the conflict state, files that need to be resolved will return to the work zone from the index.

1. Use tools or manually resolve conflicts
2. The git add command indicates that the conflict has been resolved.
3. again commit the file that has resolved the conflict.

$ Git reset -- hard ORIG_HEAD is used to unbind a committed merge.
$ Git reset -- hard HEAD is used to undo the merge without commit. The principle is to discard the index and workspace changes.

Git reset -- merge ORIG_HEAD. Note that -- hard is replaced with -- merge, which avoids clearing the working tree during rollback.

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.