Git Cheatsheet
April 14, 2018
Basics
Create repository
mkdir repocd repogit initClone repository
git clone git@github.com:user/repo.git repocd repoCommit
git add .git commit# we can stage globsgit add some_dir/**/*.js
Branching
Create
# creategit branch feature# checkoutgit checkout featuregit checkout -b featureDelete
git branch -D featureMove
# checkout any branch except the one being movedgit checkout feature2# move `feature` to `feature2`git branch -f feature feature2Forgot to create a branch
# create new branchgit checkout -b feature# move `master` back to `origin/master`git branch -f master origin/master
Merge
git checkout mastergit merge feature
Rebase
Replay
feature
commits onmaster
git checkout featuregit rebase masterMerge
git checkout mastergit merge feature
Undo
Before push
reset
modifies history.
# undo last commitgit reset --mixed HEAD~1
# undo up to and excluding `x`git reset --mixed x
After push
revert
creates a new undo commit and does not modify history.
Undo last commit.
# undo last commitgit revert HEAD
# undo x and ygit revert --no-commit x y
# undo commits between and including x and ygit revert --no-commit x..y
Restore file
git checkout x -- file
History
Search commits
git log --grep xSearch diffs
git log -S x