Skip to main content

Common commands

Omitting the --global parameter means that only this project is in effect

# Initialize this repository (create a new one)    
git init
git config --global user.name "xxx" # Configure username
git config --global user.email "xxx@xxx.com" # Configure email
git config --global color.ui true # commands such as git status automatically colour
git config --global color.status auto
git config --global color.diff auto
git config --global color.branch auto
git config --global color.interactive auto
git config --global http.proxy # View current proxy settings
git config --global http.proxy 'http://127.0.0.1:1080' # Set http proxy
git config --global https.proxy 'socks5://127.0.0.1:1080' # Set up https proxy
git config --global --unset http.proxy # remove proxy git config
git status # View current version status   
git add t.txt # Add a single file to the staging area
git add . # Add all changed files to index, not including deletes
git add -u # Add all tracked files to index only, not new files
git add -A # git add . and git add -u together
git commit -m 'xxx' # commit
git commit --amend -m 'xxx' # merge the last commit (for recurring changes)
git commit -am 'xxx' # Combine add and commit into one step
git rm xxx # Delete files from index
git rm -r * # recursively delete
git log # Show the commit log
git log -1 # show 1 line of log -n for n lines
git log --stat # Show the commit log and associated change files
git log -p -m
git log -- filename # View the changelog of a file
git show xxxx # Show details of a commit
git show dfb02 # You can use only the first few digits of the commitid
git show HEAD # Show the HEAD commit log
git show HEAD^ # Show the commit log for the last version ^^ for the last two versions ^5 for the last 5 versions
git whatchanged # Show the file changes corresponding to the commit history
git revert xxxxxx # Undo commits xxxxxx
git tag # Show existing tags    
git tag -a v2.0 -m 'xxx' # Add v2.0 tag
git show v2.0 # Show v2.0 logs and details
git log v2.0 # Show v2.0 logs
git push --tags # Push all tags to the remote repository
git tag -d tag_name # locally delete the tag named tag_name
git push origin :refs/tags/tag_name # Remotely delete the tag named tag_name
git diff # Show all changes that have not been added to index    
git diff --cached # Show all changes that have been added to index but not yet committed
git diff HEAD^ # Compare diffs with the previous version
git diff HEAD -- . /lib # Compare differences to the HEAD version of the lib directory
git diff origin/master..master # Compare remote branch master with local branch master that doesn't have
git diff origin/master..master --stat # Show only the files that differ, not the exact contents
git diff origin/master.
git clone git+ssh://git@xxx.xxx.xxx.xxx/xx.git # clone remote repository    
git remote add origin git+ssh://git@xxx.xxx.xxx.xxx/xx.git # Add remote definition (for push/pull/fetch)
git branch # Show local branches
git branch --contains 50089 # Show branches containing commit 50089
git branch -a # Show all branches
git branch -r # Show all original branches
git branch --merged # Show all branches that have been merged into the current branch
git branch --no-merged # Show all branches that have not been merged into the current branch
git branch -m master master_copy # rename local branches
git checkout -b master_copy # Create a new branch master_copy from the current branch and check it out
git checkout -b master master_copy # Full version of the above
git checkout dev/minibear2333 # Check out the existing branch
git checkout --track dev/minibear2333 # Check out the remote branch dev/minibear2333 and create a local tracked branch
git checkout v2.0 # Check out version v2.0
git checkout -b devel origin/develop # Create a new local branch devel from the remote branch devel and check it out
git checkout -- README # Check out the README file for the head version (can be used to roll back changes)
git merge origin/master # Merge the remote master branch into the current branch
git cherry-pick xxxxxx # merge commit xxxxxx changes
git push origin master # push the current branch to the remote master branch
git push origin :dev/minibear2333 # Delete the dev/minibear2333 branch from the remote repository
git fetch # fetch all remote branches (does not update local branches, requires merge)
git fetch --prune # fetch all original branches and clear any deleted branches on the server
git pull origin master # Get the remote branch master and merge it to the current branch
git mv README README2 # Rename the file README to README2
git reset --hard HEAD # Reset the current version to HEAD (usually used for merge failures)
git rebase
git branch -d dev/minibear2333 # Delete the branch dev/minibear2333 (you need to make sure the changes in this branch have been merged into other branches)
git branch -D dev/minibear2333 # Force delete branch dev/minibear2333, be careful
git ls-files # List the files contained in the git index
git show-branch # Graph the current branch history
git show-branch --all # Show the history of all branches
git show-branch

Graphical commands

git ls-tree HEAD # Internal command: show a git object    
git rev-parse v2.0 # Internal command: show SHA1 HASH for a ref
git reflog # Show all commits, including isolated nodes
git show xxx # See what files have been changed by the xxx commit
git show HEAD # Show the status of the current branch as of yesterday
git log --pretty=format:'%h %s' --graph # Graph the commit log
git show HEAD~3 # See what was changed on the penultimate commit
git show -s --pretty=raw xxxxxx
git stash # Stash current changes, put all to HEAD    
git stash list # View all staging
git stash show -p stash@{0} # Refer to first staging
git stash apply stash@{0} # Apply first stash

Find

git grep "delete from" # Find the contents of the file under the current branch, you can git grep --help to see the exact usage                              
git grep "delete from" v2.0 # Specify a tag to look for

git index operation (trace)

git update-index -assume-unchanged filename # untrack locally    
git update-index -no-assume-unchanged filename # Restore local tracking
git ls-files -v| grep '^h\ ' # You can see the files that are not tracked locally

# Manage remote branches

git remote # List existing remote branches without arguments                          
git remote -v # (-v is short for -verbose, take the first letter) lists the details and lists the remote url after each name
git remote add [shortname] url # Add a remote repository
git fetch origin # The string origin refers to the address of the corresponding repository. For example, to fetch all the information that is available in origin but not in the local repository, you can use

tagging

git tag #List all tags
git tag -l "v1*" #Support fuzzy matching: git tag v2.3
git tag v2.3 #Create tags
git tag v2.4 -a -m "test" #Create lightweight tags ``-a` argument can be omitted
git show v2.4 #View annotated tags
git tag -a v2.5 9fceb02 #History of commits with `tag`
git push origin v1.0 #push individual tags
git push origin --tags #Push all tags
git tag -d v1.0 #delete tags
git push origin --delete v1.0 #delete remote tags
git checkout v1.1 #Checkout tags locally
git checkout -b v1.1-dev v1.1 #Checkout tags to branch
git checkout