Git cheat sheet by task
Find the command by what you want to do, with a note on whether it is safe, rewrites history or discards work.
git reset --soft HEAD~1git reset HEAD~1git reset --hard HEAD~1git commit --amend -m "new message"git add file && git commit --amend --no-editgit restore --staged filegit restore filegit switch -c feature/namegit switch -git branch -m new-namegit branch -d namegit push origin --delete namegit stash push -u -m "wip" && git stash popgit fetch origin && git rebase origin/maingit fetch origin && git merge origin/maingit rebase -i HEAD~3git cherry-pick <sha>git revert <sha>git push --force-with-leasegit log --oneline --graph --decorate --allgit diff --stagedgit log origin/main..HEAD --onelinegit blame -L 10,30 filegit bisect start && git bisect bad && git bisect good <sha>git log -S"text" --onelinegit refloggit rm --cached filegit clean -n then git clean -fdgit remote set-url origin <url>git push -u origin HEADgit clone --depth 1 <url>git tag -a v1.2.0 -m "Release 1.2.0" && git push origin v1.2.0git submodule update --init --recursivegit show <sha>:path/to/fileGit by task
Most people use a dozen Git commands daily and look up the rest every time: how to undo the last commit without losing the work, how to unstage, how to clean up a branch before a pull request. Git's help pages are organised by command; when you are stuck you think in terms of what you want to do. This sheet is organised by task.
Each entry gives the command, a one-line explanation of what it does and a risk label so you know what you are about to do: safe (nothing is lost), rewrites history (fine for local commits, dangerous after pushing without coordination) or discards work (uncommitted changes cannot be recovered). Use the search box to filter, for example "undo", "branch" or "stash".
Three habits that prevent most disasters
- Commit often. Committed work can almost always be recovered through the reflog; uncommitted work cannot.
- Before a command that discards work, look first:
git status,git diffand the-ndry-run flag ongit clean. - Use
git revertfor anything already pushed to a shared branch, and--force-with-leasefor any force push you must make.
Commands use the modern git switch and git restore introduced in Git 2.23 alongside the older checkout. Test unfamiliar commands in a scratch repository first. The page is static and works offline.
Frequently asked questions
What is the difference between git reset and git revert?
reset moves the branch pointer and rewrites history, which is fine for local commits. revert adds a new commit that undoes an earlier one, which is the right tool for commits that others may already have.
What is the difference between merge and rebase?
Merge joins two histories with a merge commit and keeps everything as it happened. Rebase replays your commits on top of another branch for a straight line, but creates new commits, so never rebase commits others have based work on.
I deleted a branch or lost a commit. Is it gone?
Usually not. git reflog records every position of HEAD for about 90 days by default; find the commit there and create a branch pointing to it.
Why use --force-with-lease?
A plain force push overwrites whatever is on the remote. With lease, Git first checks that the remote branch is where you last saw it and refuses otherwise, protecting a colleague's newer commits.