Git Cheat Sheet
Git References
HEAD
: The latest commit
HEAD^
/ HEAD~1
: The previous commit before the latest commit
HEAD~n
: The previous n commit(s) before the latest commit
Clone
Clone an existing repository
$ git clone <git_url>
Initialize
Initialize a new repository
$ git init
Remote
List all remotes
$ git remote -v
Show information about remote
$ git remote show <remote>
Add a new remote
repository
$ git remote add <remote> <git_url>
Status
Show files status
$ git status
Differences
Show differences
$ git diff
Show changed status for each file
$ git diff --name-status
Show all merged conflicted files name
$ git diff --name-only --diff-filter=U
[Note] diff-so-fancy or vimdiff are your good friends when reading a long diff file
Add
Add files
for next commit
$ git add <file>
Add all files under current directory for next commit
$ git add .
Add all files under current directory except file
(or files under path
)
$ git add .
$ git reset -- <file|path>
Add all files in entire project for next commit
$ git add --all
Remove
Remove file
from the working tree
$ git rm <file>
Commit
Commit all added files
$ git commit
Modify the latest commit
$ git commit --amend
Tag
List all tags
$ git tag
Mark current commit with a tag
$ git tag <tag>
Sort all tags by time
$ git tag | xargs -I@ git log --format=format:"%ci %h @%n" -1 @ | sort
Stash
Save changes away and revert back to the HEAD
commit
$ git stash
List all stashes
$ git stash list
Pop a stash and apply it on top of the current working tree
$ git stash pop
Log
Show entire commits log
$ git log
Show commits log for a file
$ git log <file>
Show author and modification date for a file
$ git blame <file>
Branch
List all existing branches
$ git branch -av
Delete a branch
locally
$ git branch -d <branch>
Delete a branch
on the remote
$ git branch -dr <remote/branch>
Create a new branch
$ git branch <branch>
Checkout
Switch to branch
$ git checkout <branch>
Create and switch to a new branch
based on current branch
$ git checkout -b <branch>
Create and switch to a new branch
based on a remote
branch
$ git checkout --track <remote/branch>
Pull
Fetch and merge all changes from a remote
and branch
$ git pull <remote> <branch>
Pull the latest changes form original repository
$ git remote add upstream <git_url>
$ git pull upstream <branch>
Push
Push commits to a remote
and branch
$ git push <remote> <branch>
Force push revision to a remote
and branch
$ git push <remote> <branch> --force
Push tags
$ git push --tags
Merge
Merge branch
to current branch
$ git merge <branch>
Reset
Discard all changes in current working tree
$ git reset --hard HEAD
Discard all changes in current working tree since a commit
$ git reset --hard <commit>
Reset commit to a commit
and preserve changes as unstaged
$ git reset <commit>
Cherry-pick
Apply the changes from a commit
$ git cherry-pick <commit>
Rebase
Rebase your HEAD
commit on to a branch
$ git rebase <branch>
Warning: DO NOT do rebase if you are not sure what you are doing.
Submodule
Initialize submodules recorded
$ git submodule init
Update submodules by cloning missing submodules
$ git submodule update
Update every submodule to latest commit on origin
$ git submodule foreach --recursive git pull origin master