Git Cheat Sheet
Everyday Git workflows, branching, rebasing, recovery, and history inspection commands for engineers working in real repositories.
Setup & Config
6 commandsgit config --global user.name "Name"Set name
git config --global user.email "e@x.com"Set email
git initInitialize new repo
git clone URLClone remote repo
git remote -vList remotes
git remote add origin URLAdd remote
Basic Workflow
7 commandsgit statusWorking tree status
git add file.txtStage a file
git add .Stage all changes
git commit -m 'msg'Commit staged changes
git push origin mainPush to remote
git pull origin mainPull latest changes
git fetch originDownload without merge
Branching
9 commandsgit branchList local branches
git branch -aList all branches
git branch featureCreate branch
git checkout featureSwitch branch
git checkout -b featureCreate & switch
git switch featureSwitch (modern)
git switch -c featureCreate & switch (modern)
git branch -d featureDelete merged branch
git branch -D featureForce delete branch
Merging & Rebasing
6 commandsgit merge featureMerge branch into current
git merge --no-ff featureMerge with commit
git rebase mainRebase onto main
git rebase -i HEAD~3Interactive rebase (squash)
git merge --abortAbort failed merge
git rebase --abortAbort failed rebase
Stashing
6 commandsgit stashSave working changes
git stash popApply & remove stash
git stash applyApply, keep stash
git stash listList all stashes
git stash dropDelete latest stash
git stash -uStash including untracked
Viewing History
8 commandsgit log --onelineCompact log
git log --graph --oneline --allVisual branch graph
git log -p file.txtFile change history
git diffUnstaged changes
git diff --stagedStaged changes
git diff branch1..branch2Compare branches
git show HASHShow specific commit
git blame file.txtWho changed each line
Undoing Things
7 commandsgit checkout -- file.txtDiscard file changes
git restore file.txtDiscard (modern)
git reset HEAD file.txtUnstage a file
git reset --soft HEAD~1Undo commit, keep staged
git reset --hard HEAD~1Undo commit, discard all
git revert HASHCreate undo commit
git reflogRecovery log (find lost commits)
Cherry-Pick & Tags
5 commandsgit cherry-pick HASHApply specific commit
git tag v1.0Create lightweight tag
git tag -a v1.0 -m 'msg'Create annotated tag
git push origin --tagsPush all tags
git tag -d v1.0Delete local tag
Worktrees
4 commandsgit worktree add ../feature-wt featureCreate worktree for branch
git worktree listList all worktrees
git worktree remove ../feature-wtRemove worktree
git worktree pruneClean stale worktrees
Bisect (Find Bug Commits)
5 commandsgit bisect startBegin binary search
git bisect badMark current as broken
git bisect good HASHMark known good commit
git bisect run ./test.shAuto-bisect with script
git bisect resetEnd bisect session
Submodules
5 commandsgit submodule add URL pathAdd submodule
git submodule update --init --recursiveClone submodules
git submodule foreach git pullUpdate all submodules
git submodule statusShow submodule commits
git rm --cached path && rm -rf pathRemove submodule
Hooks & Automation
6 commands.git/hooks/pre-commitRuns before each commit
.git/hooks/pre-pushRuns before each push
.git/hooks/commit-msgValidate commit message
chmod +x .git/hooks/pre-commitMake hook executable
npx husky installHusky: managed git hooks
npx lint-stagedRun linters on staged files
Advanced Recovery
7 commandsgit reflogFull history of HEAD movements
git fsck --lost-foundFind dangling commits
git stash drop stash@{0}Delete specific stash
git checkout HASH -- file.txtRestore file from commit
git reset --hard ORIG_HEADUndo last reset/merge
git clean -fdRemove untracked files+dirs
git gc --prune=nowGarbage collect objects
Interactive & Patch Mode
6 commandsgit add -pStage hunks interactively
git checkout -pDiscard hunks interactively
git stash -pStash specific hunks
git rebase -i HEAD~5Squash/reorder last 5 commits
git commit --fixup=HASHMark as fixup for rebase
git rebase -i --autosquashAuto-apply fixup commits