This git cheat sheet is organised by task, not by command, because nobody searches for “git reset --soft”; they search for “undo last commit”. Find what you are trying to do, copy the command, and read the one-line explanation of what it changes. Search ranks tasks first, so “delete remote branch” or “rename branch” goes straight to the right entry.
Commands that rewrite history or throw work away are marked, with a note on what can be recovered: almost anything that was ever committed can be found again with git reflog, while uncommitted edits you discard with restore, reset --hard or clean are gone. Where Git now has a clearer command (git switch and git restore since 2.23, git config set and list since 2.46), that is shown first with the classic form alongside, so the sheet works whether your team learned Git last year or in 2010. Checked against the git-scm.com reference for Git 2.56.
How to use
- Type what you want to do in the search box, such as “undo last commit”, “unstage” or “squash commits”, or click one of the common tasks.
- Read the badges before you run anything: amber means the command rewrites history, red means it can discard work. The recovery note says how to get back.
- Click Copy command. The copy leaves out the # comments so it pastes cleanly into a terminal; replace placeholders such as a1b2c3d or feature/login with your own.
- Share a specific answer with the # link next to each task, or print the whole sheet as a two-column PDF.
Frequently asked questions
How do I undo the last commit in git?
If you have not pushed it, git reset --soft HEAD~1 removes the commit and keeps its changes staged, and git reset HEAD~1 keeps them unstaged. git reset --hard HEAD~1 removes the commit and its changes. If the commit is already pushed to a shared branch, use git revert HEAD instead, which adds a new commit that undoes it without rewriting anyone's history.
What is the difference between git reset, git restore and git revert?
git restore changes files: it discards edits in the working tree or unstages them, and never moves a branch. git reset moves the current branch to another commit (and with --hard also overwrites your files), so it rewrites history. git revert creates a new commit that applies the opposite of an earlier one; history only grows, which makes it the safe choice on branches other people use.
How do I rename a git branch, locally and on the remote?
git branch -m new-name renames the branch you are on (git branch -m old new for another one). Remotes have no rename operation, so push the new name with git push -u origin new-name and delete the old one with git push origin --delete old-name. On GitHub, renaming the branch in the web interface also retargets open pull requests.
Should I use git switch or git checkout?
Either works. git checkout does two unrelated jobs, switching branches and restoring files, which is why Git 2.23 split them into git switch and git restore. The new commands are clearer and harder to misuse (git checkout -- file silently discards edits), so this sheet shows them first. git checkout is not deprecated and every command here lists its classic equivalent.
Can I recover a commit after git reset --hard?
Usually, yes. Run git reflog, find the line from before the reset, and run git reset --hard HEAD@{n} or create a branch on that SHA. Commits stay reachable through the reflog for 30 days by default once nothing else points to them. What cannot be recovered is uncommitted work: changes that were never committed or stashed are overwritten for good.
What does git push --force-with-lease do?
It overwrites the remote branch only if the remote still points where your last fetch saw it. If someone pushed in the meantime, the push is refused instead of silently deleting their commits, which is what plain --force does. Use it after a rebase or amend on a branch you have already pushed.
Which undo command? Decide by where the change is
• Edited a file and want the edit gone: git restore file. Not recoverable, because it was never committed.
• Staged something by mistake: git restore --staged file. Nothing is lost; the edit stays in the file.
• Committed but not pushed: git reset --soft HEAD~1 to redo the commit, or git commit --amend to fix it in place.
• Committed and pushed to a shared branch: git revert <sha>. Do not reset a branch other people have pulled.
• Lost a commit, a branch or the result of a bad rebase: git reflog, then git branch rescued <sha>.
The rule underneath: anything that rewrites commits is fine while the commits are only yours, and becomes everyone's problem once they are shared.
Merge or rebase?
Both bring the changes from one branch into another. A merge records that two histories joined, with a merge commit that has two parents; nothing is rewritten, so it is always safe on shared branches. A rebase replays your commits on top of the other branch, producing a straight line of history as if you had started from the latest commit, but it creates new commit IDs, so anyone who already has the old ones must reconcile.
A common team convention: rebase your own feature branch onto main to stay current (git pull --rebase, or git rebase main), and merge the finished branch into main through a pull request. Squash-merging gives one commit per feature on main; plain merges keep every commit and the branch shape. Pick one per repository and set it in the host's settings, so history stays consistent.
Git settings worth changing from the defaults
• pull.rebase true: git pull rebases instead of creating merge commits for every sync.
• push.autoSetupRemote true (Git 2.37+): the first git push of a new branch sets its upstream, no -u needed.
• merge.conflictStyle zdiff3 (Git 2.35+): conflict markers include the common ancestor, which makes most conflicts obvious.
• rerere.enabled true: Git remembers how you resolved a conflict and reapplies it when the same conflict comes back during a rebase.
• fetch.prune true: remote branches deleted on the server disappear locally on the next fetch.
• init.defaultBranch main: new repositories start on main.
