Following the last time, I took the time to sort out a relatively complete list of commonly used Git commands, and found a great map, very high-definition (1759*3162).

View, add, submit, delete, retrieve, reset modified files

git help # Display help for command

git show # Display the contents of a certain commit git show $id

git co – # Discard workspace modifications

git co . # Discard workspace modifications

git add # Submit the working file modifications to the local staging area

git add . # Submit all modified working files to the staging area

git rm # Delete files from the repository

git rm –cached # Delete the file from the repository, but not delete the file

git reset #Restore from the staging area to the working file

git reset – . # Restore from the staging area to the working file

git reset –hard # Restore the state of the last submission, that is, abandon all modifications since the last submission

git ci git ci . git ci -a # Combine operations such as git add, git rm and git ci together to do git ci -am “some comments”

git ci –amend # Modify the last commit record

git revert <$id> # Restore the status of a certain submission. The restore action itself also creates the submission object.

git revert HEAD #Restore the status of the last commit

View file diff

git diff # Compare the differences between the current file and the staging area file git diff

git diff <$id1> <$id2> # Compare the differences between two commits

git diff .. # Compare between two branches

git diff –staged # Compare the differences between the staging area and the repository

git diff –cached # Compare the differences between the staging area and the repository

git diff –stat # only compare statistics

View submission history

git log git log # View each commit record of this file

git log -p # View the diff of each detailed modification

git log -p -2 # View the diff of the last two detailed modifications

git log –stat #View submission statistics

tig

You can use tig on Mac instead of diff and log, brew install tig

Git local branch management

View, switch, create and delete branches

git br -r # View remote branches

git br <new_branch> # Create a new branch

git br -v # View the last commit information of each branch

git br –merged # View branches that have been merged into the current branch

git br –no-merged # View branches that have not been merged into the current branch

git co # Switch to a branch

git co -b <new_branch> # Create a new branch and switch to it

git co -b <new_branch> # Create a new new_branch based on branch

git co $id # Checkout a certain historical commit record, but there is no branch information. Switching to other branches will automatically delete it.

git co $id -b <new_branch> # Checkout a certain historical commit record and create a branch

git br -d # Delete a branch

git br -D # Forced deletion of a branch (needs to be forced when unmerged branches are deleted)

Branch merging and rebase

git merge # Merge branch into current branch

git merge origin/master –no-ff # Do not Fast-Foward merge, so that merge submission can be generated

git rebase master # Rebase master to branch, equivalent to: git co && git rebase master && git co master && git merge

Git patch management (convenient for synchronization of development on multiple machines)

git diff > ../sync.patch # Generate patch

git apply ../sync.patch # Apply patch

git apply –check ../sync.patch #Test whether the patch is successful

Git staging management

git stash # temporary storage

git stash list # List all stash

git stash apply # Restore temporary contents

git stash drop # Delete the staging area

Git remote branch management

git pull # Grab all branch updates from the remote warehouse and merge them locally

git pull –no-ff # Grab all branch updates from the remote warehouse and merge them locally. Do not fast-forward the merge.

git fetch origin # Fetch remote warehouse updates

git merge origin/master # Merge the remote master branch into the local current branch

git co –track origin/branch # Track a remote branch and create a corresponding local branch

git co -b <local_branch> origin/<remote_branch> # Create a local branch based on the remote branch, the function is the same as above

git push # push all branches

git push origin master # Push the local master branch to the remote master branch

git push -u origin master # Push the local master branch to the remote (if there is no remote master branch, create it to initialize the remote warehouse)

git push origin <local_branch> # Create a remote branch, origin is the name of the remote warehouse

git push origin <local_branch>:<remote_branch> # Create a remote branch

git push origin :<remote_branch> #Delete the local branch first (git br -d ), then push to delete the remote branch

Git remote warehouse management

GitHub

git remote -v # View the remote server address and warehouse name

git remote show origin # View the remote server warehouse status

git remote add origin git@ github:robbin/robbin_site.git # Add remote warehouse address

git remote set-url origin git@ github.com:robbin/robbin_site.git # Set the remote warehouse address (used to modify the remote warehouse address) git remote rm # Delete the remote warehouse

Create a remote warehouse

git clone –bare robbin_site robbin_site.git # Create a version-only repository using a versioned project

scp -r my_project.git git@ git.csdn.net:~ # Upload the pure warehouse to the server

mkdir robbin_site.git && cd robbin_site.git && git –bare init # Create a pure warehouse on the server

git remote add origin git@ github.com:robbin/robbin_site.git # Set the remote warehouse address

git push -u origin master # Client’s first submission

git push -u origin develop # Submit the local develop branch to the remote develop branch for the first time, and track

git remote set-head origin master # Set the HEAD of the remote warehouse to point to the master branch

You can also command to set up tracking remote libraries and local libraries

git branch –set-upstream master origin/master

git branch –set-upstream develop origin/develop

Warm reminder: If you want to enlarge the image, please open it in a new window.