Git Command Cheatsheet

Searchable reference of everyday Git commands for branching, undoing and inspecting history.

Setup & clone
git initStart a new repository
git clone <url>Clone a remote repository
git remote -vList remotes
git remote add origin <url>Add a remote
Daily flow
git status -sbShort status with branch info
git add -pStage changes hunk by hunk
git commit -m "msg"Commit staged changes
git commit --amend --no-editAdd to the last commit
git push -u origin HEADPush and track current branch
git pull --rebasePull and replay local commits
Branching
git switch -c feature/xCreate and switch to a branch
git switch mainSwitch branch
git branch -d feature/xDelete merged branch
git merge --no-ff feature/xMerge with a merge commit
git rebase mainRebase onto main
git cherry-pick <sha>Apply one commit elsewhere
Undo & rescue
git restore <file>Discard working changes
git restore --staged <file>Unstage a file
git reset --soft HEAD~1Undo last commit, keep changes
git reset --hard origin/mainForce local to match remote
git revert <sha>Create an inverse commit
git reflogFind lost commits
git stash push -m "wip"Stash work in progress
git stash popRestore stashed work
Inspect
git log --oneline --graph --allVisual commit graph
git diff --stagedDiff staged changes
git blame -L 10,20 <file>Who changed these lines
git show <sha>Inspect a commit
git bisect startBinary-search a bad commit
Cleanup
git clean -fdDelete untracked files & dirs
git gc --prune=nowGarbage collect
git fetch --pruneDrop deleted remote branches
git rm --cached <file>Stop tracking a file

Related tools

All →