Git Cheat Sheet

Distributed version control system commands. Essential for every developer.

Top Content Ad (Responsive)

Setup & Local

Create & Clone
  • git init Initialize new repo
  • git clone [url] Clone remote repo
  • git clone [url] [dir] Clone into specific folder
Local Changes
  • git status Check changes
  • git diff View unstaged changes
  • git add . Stage all changes
  • git add [file] Stage specific file
  • git commit -m "msg" Commit with message
  • git commit -am "msg" Stage & Commit tracked
  • git commit --amend Change last commit
Undo & Reset
  • git checkout -- [file] Discard file changes
  • git reset HEAD [file] Unstage file
  • git revert HEAD Revert last commit (new commit)
  • git reset --hard HEAD Discard all local changes
  • git rm [file] Remove file

Branching & History

Branches & Tags
  • git branch List branches
  • git branch [name] Create new branch
  • git checkout [name] Switch branch
  • git checkout -b [name] Create & Switch
  • git branch -d [name] Delete branch
  • git tag [name] Tag current commit
History
  • git log Show all commits
  • git log --oneline Short format
  • git log -p [file] Show changes for file
  • git log --stat Show stats
  • git blame [file] Who changed what

Remote & Merge

Remote
  • git remote -v List remotes
  • git remote add [alias] [url] Add remote
  • git fetch [alias] Fetch changes
  • git pull [alias] [branch] Fetch + Merge
  • git push [alias] [branch] Publish commits
  • git push [alias] :[branch] Delete remote branch
Merge & Rebase
  • git merge [branch] Merge into current
  • git rebase [branch] Rebase onto branch
  • git rebase --abort Abort rebase
  • git rebase --continue Continue after resolving
  • git mergetool Open merge tool
Conflicts
  • git diff View conflict diff
  • git diff --base [file] Against base
  • git diff --ours [file] Against your changes
  • git diff --theirs [file] Against incoming
  • git add [file] Mark resolved

Advanced

Bisect (Find Bugs)
  • git bisect start Start bisecting
  • git bisect good [commit] Mark good commit
  • git bisect bad [commit] Mark broken commit
  • git bisect reset Stop bisecting
Maintenance & Search
  • git fsck Check for errors
  • git gc --prune Cleanup & Optimize
  • git grep "text" Search in files
Bottom Content Ad (Responsive)