~/cheat-sheets / git
Git cheat sheet
Git commands for everyday work and emergencies – searchable and copyable in one click. With warnings on commands that discard work for good.
Download as PDF (A4, free to share) ↓
37 commands
Everyday: status & commit
Short status with branch and tracking info
git status -sbShow unstaged changes
git diffShow staged changes
git diff --stagedStage changes hunk by hunk
git add -pCreate a commit with a message
git commit -m "Fix login redirect"Add staged changes to the last commit
⚠ Careful: changes or deletes datagit commit --amend --no-editCreate and switch to a new branch
git switch -c feature/login
Log & search
Last 20 commits as a compact graph
git log --oneline --graph --decorate -20One author's commits from the last 2 weeks
git log --since='2 weeks ago' --author='Daniele'Find commits that added or removed a string
git log -S 'password' --onelineWho last changed lines 10-30?
git blame -L 10,30 file.py
Stash
Stash changes with a description
git stash push -m "wip login"Also stash untracked files
git stash push -uApply a stash and keep it
git stash apply stash@{0}Apply the newest stash and remove it
git stash popDiscard one stash
⚠ Careful: changes or deletes datagit stash drop stash@{0}
Rebase & tidying history
Replay the current branch onto main
git rebase mainInteractively reorder, squash or reword the last 4 commits
⚠ Careful: changes or deletes datagit rebase -i HEAD~4Abort the rebase, back to the previous state
git rebase --abortMerge a branch with a merge commit
git merge --no-ff feature/login
Undoing things
Discard unstaged changes of a file
⚠ Careful: changes or deletes datagit restore file.txtUnstage a file, keep the changes
git restore --staged file.txtUndo a commit with a new inverse commit (safe on shared branches)
git revert abc1234Undo the last commit, keep changes staged
git reset --soft HEAD~1Throw away the last commit and its changes
⚠ Careful: changes or deletes datagit reset --hard HEAD~1Permanently delete untracked files and folders
⚠ Careful: changes or deletes datagit clean -fd
Reflog & recovery
Show the history of all HEAD movements
git reflogReset to an earlier reflog state
⚠ Careful: changes or deletes datagit reset --hard HEAD@{2}Rescue a lost commit by hash as a branch
git switch -c rescue abc1234Find dangling objects
git fsck --lost-foundUndo the last reset, merge or rebase
⚠ Careful: changes or deletes datagit reset --hard ORIG_HEAD
Remotes & bisect
Fetch all remotes, prune deleted branches
git fetch --all --pruneFetch and replay local commits on top
git pull --rebaseForce push that won't overwrite others' new commits
⚠ Careful: changes or deletes datagit push --force-with-leaseStart a binary search for the breaking commit
git bisect start && git bisect bad && git bisect good v1.2Run bisect automatically with a test script
git bisect run ./test.shEnd bisect, return to the original branch
git bisect reset
No match.
New articles by e-mail
One e-mail per new article. No tracking, unsubscribe any time. As a thank-you after confirming: my “Server operations & patching” checklist (PDF).