Git Cheat Sheet
Explore key concepts, syntax, and usage examples for Git — perfect for quick reference.
Resource
Online Related
Create Git
From existing directory
- cd project_dir
- git init
- git add .
- git clone existing_dir new_dir
- git clone git://github.com/user/repo.git
- git clone https://github.com/user/repo.git
Local Changes
Changed in working directory
- git status
- git diff
- git add file1 file2 file3
- git rm file
- git rm dir/ -r
💡 (recursive under directory)
- git diff --cached
- git commit
- git commit -m "My message"
- git commit -a -m "My Message"
💡 (tracked files only, auto add)
- git commit --amend
- git checkout -- file
- git revert HEAD
- git reset --hard HEAD
History
Show all commits
- git log
- git log --pretty=-short
- git log -p
- git log file
- git log dir/
- git log --stat
- git blame file
Merge/Rebase
Merge branch into current
- git merge branch
- git rebase branch
- git rebase master branch
- git rebase --abort
- git mergetool
- git diff
💡 complete conflict diff - git diff --base $file
💡 against base file - git diff --ours $file
💡 against your changes - git diff --theirs $file
💡 against other changes
- git reset --hard
- git rebase --skip
- git add $conflicting_file
💡 do for all resolved files - git rebase --continue
Remote Update / Publish
List remotes
- git remote -v
- git remote show remote
- git remote add path/url
- git fetch remote
- git pull remote branch
- git push remote branch
- git push remote :branch
- git push origin/upstream --tags
Branching/Tagging
List branches
- git branch
- git checkout branch
- git branch new
- git branch new existing
- git branch -d branch
- git tag tagname
Useful Commands
Finding Regressions
- git bisect start
💡 to start - git bisect good $id
💡 $id is the last working version - git bisect bad $id
💡 $id is a broken version - git bisect bad/good
💡 to mark it as bad or good - git bisect visualize
💡 to launch gitk and mark it - git bisect reset
💡 once you're done
- git fsck
- git gc --prune
- git grep "foo()"