Git Cheat Sheet: Switch, Restore, Undo, Rebase and Bisect
Checked against Git Git 2.56.1 docs; examples run on Git 2.50.1 on 28 November 2026. Each section links to the official docs it was checked against.
This sheet is for developers who know add, commit and push and now want the commands that get them out of trouble. It follows the Git 2.56.1 documentation (released 26 Oct 2026) and every local example was run on Git 2.50.1 in a throwaway repository. Rows that need a real remote are marked as checked in the docs only.
When to use what
Use git switch to move between branches and git restore to throw away or unstage file changes. checkout still does both jobs, which is exactly why it bites: git checkout name means a branch if one exists and a file if it doesn't. The two newer commands each do one thing, so a typo gives you an error instead of lost work.
For undoing, pick by where the mistake lives. If it is only on your machine, reset rewrites history: --soft keeps changes staged, the default --mixed keeps them in the working tree, and --hard deletes them. If you already pushed, use revert, which adds a new commit that cancels the old one and doesn't break anyone else's clone. If you think you lost something, run git reflog before you panic; it records where HEAD has been for about 90 days by default.
Rebase versus merge is mostly team taste. I rebase my own feature branch onto main before opening a pull request and never rebase a branch someone else has pulled. The rebase vs merge post goes through the trade-offs, and the interactive rebase planner lets you draft a todo list before you touch a real branch. If you can't remember the flags, the Git command generator builds the command from a description.
restore lives at https://git-scm.com/docs/git-restore. Both commands are still labelled experimental in the man pages, but they have been stable in practice since Git 2.23.
git switch featureMove to an existing local branch, or create one tracking origin/feature if only the remote branch exists
git switch -c fix/loginCreate a new branch from HEAD and move onto it in one step
git switch -Jump back to the branch you were on before the last switch
git switch --detach v2.1.0Check out a tag or commit without a branch, for a quick look or build
git restore app.jsThrow away unstaged edits to a file and go back to the staged or committed version
git restore --staged app.jsUnstage a file but keep your edits in the working tree
git restore --source=HEAD~2 app.jsBring back a file as it was two commits ago without moving HEAD
See also https://git-scm.com/docs/git-revert and https://git-scm.com/docs/git-reflog. HEAD~1 means one commit before HEAD.
git commit --amend --no-editAdd staged changes to the last commit and keep its message
git commit --amend -m "fix: handle empty login form"Replace the last commit's message without changing its content
git rm --cached .envStop tracking a file you committed by mistake but keep it on disk; add it to .gitignore next
git reset --soft HEAD~1Undo the last commit but keep its changes staged, ready to commit again
git reset HEAD~1Undo the last commit and leave its changes unstaged in the working tree
git reset --hard HEAD~1Delete the last commit and its changes from the branch and working tree
Uncommitted work is gone; committed work can still be found with reflog.
git revert --no-edit HEADAdd a new commit that cancels the last one, safe on shared branches
git reflog -10Show the last ten places HEAD pointed to, including commits you reset away
git reset --hard HEAD@{2}Move the branch back to where HEAD was two moves ago, after reading the reflog
git branch -vvList local branches with their upstream and ahead/behind counts
git branch --merged mainList branches whose commits are all in main, so they are safe to delete
git branch -d old-featureDelete a branch only if it is merged; refuses with 'not fully merged' otherwise
git branch -D old-featureForce-delete a branch even if its commits are not merged anywhere
git branch -m new-nameRename the current branch
git push -u origin HEADPublish the current branch under the same name and set it as upstream
In the interactive todo list, pick keeps a commit, reword edits its message, squash merges it into the one above and combines messages, fixup merges it and drops its message, and drop deletes it. Lines run top to bottom, oldest first.
git rebase mainReplay your branch's commits on top of the current main
git rebase -i HEAD~3Open the last three commits in your editor to reorder, squash, reword or drop them
git commit --fixup=abc1234Make a commit marked to be folded into commit abc1234 later
git rebase -i --autosquash mainStart an interactive rebase with fixup! commits already moved under their targets
git rebase --continueCarry on after you fixed a conflict and staged the files
git rebase --abortCancel the rebase and put the branch back exactly as it was
git stash push -m "wip login"Save tracked changes with a label and clean the working tree
git stash -uStash untracked files too, not only tracked ones
git stash listShow saved stashes as stash@{0}, stash@{1} and so on, newest first
git stash popApply the newest stash and delete it if it applied cleanly
git stash apply stash@{1}Apply an older stash and keep it in the list
git stash show -p stash@{0}Print the full diff stored in a stash before applying it
git bisect start HEAD v1.4.0Start a bisect with HEAD as bad and tag v1.4.0 as the last known good
git bisect goodMark the checked-out commit as working; Git halves the range again
git bisect badMark the checked-out commit as broken
git bisect run npm testLet a script decide: exit 0 is good, 1 to 127 (except 125) is bad, 125 skips
git bisect resetEnd the session and return to the branch you started on
These need a real remote, so they were checked against the docs only.
git remote -vShow each remote's fetch and push URLs
git remote set-url origin [email protected]:me/app.gitSwitch a remote from HTTPS to an SSH URL
git fetch --pruneDownload new commits and delete remote-tracking branches that were removed upstream
git pull --rebaseFetch and rebase your local commits on top instead of making a merge commit
git push --force-with-leaseOverwrite the remote branch only if nobody pushed since your last fetch
git log --oneline --graph --allDraw every branch as a compact text graph
git log --oneline main..featureList commits on feature that are not on main yet
git log -S "apiKey" --onelineFind commits that added or removed the string apiKey
git log -p --follow -- src/app.jsShow every change to one file, following it across renames
git diff --stagedShow what will go into the next commit
git diff --stat HEAD~2Summarise files and line counts changed in the last two commits
git blame -L 40,60 src/app.jsShow who last changed lines 40 to 60 of a file and in which commit
Worked example: Squash a messy branch, then find a regression with bisect
Say your last three commits are c3, c4 and c5, and c4 was a quick follow-up to c3. Fold it in:
git rebase -i HEAD~3
# in the editor, change line 2 from 'pick <c4>' to 'fixup <c4>', save, close
git log --oneline -4
# 210cea6 c5
# 02e18c3 c3
# f1f6612 c2
# b476845 c1
The hashes of c3 and c5 changed because rebase writes new commits. The old ones are still reachable through git reflog if you change your mind.
Now a test fails on main and you know it passed five commits ago:
git bisect start HEAD HEAD~4
# Bisecting: 1 revision left to test after this (roughly 1 step)
# run your test, then:
git bisect good # or: git bisect bad
# ... d3233e2 is the first bad commit
git bisect reset
In our test repository four commits took two checks to narrow down, because bisect halves the range each time. On a range of 1,000 commits you need about ten checks. When it prints 'is the first bad commit', read that commit's diff, then run git bisect reset so you are back on your branch and not sitting on a detached HEAD.
Common mistakes
Running
git reset --hardwith uncommitted work you wanted to keep.Reset only touches committed history safely; stash or commit first. If the work was committed,
git reflogshows the old hash andgit reset --hard <hash>brings it back.Rebasing or amending a branch that teammates already pulled, then force-pushing.
Use
git reverton shared branches. If you must rewrite your own pushed branch, use--force-with-lease, which refuses when someone else pushed in the meantime.Using
git checkout nameand getting a file reverted when you meant a branch, or the other way round.Use
git switchfor branches andgit restorefor files. When you do use checkout for files, put--before the path.Forgetting
git bisect resetand then committing on a detached HEAD.Always end with
git bisect reset. If you already committed there, create a branch on it withgit switch -c rescuebefore switching away.Expecting
git stashto save new files, then finding them still in the working tree or lost after a clean.Plain stash ignores untracked files. Use
git stash -u, or-aif you also need ignored files.
Related cheat sheets
Sources
- https://git-scm.com/docs/git-switch
- https://git-scm.com/docs/git-restore
- https://git-scm.com/docs/git-reset
- https://git-scm.com/docs/git-revert
- https://git-scm.com/docs/git-reflog
- https://git-scm.com/docs/git-rebase
- https://git-scm.com/docs/git-stash
- https://git-scm.com/docs/git-bisect
- https://git-scm.com/docs/git-push
- https://git-scm.com/docs/git-log
- https://git-scm.com/docs/BreakingChanges
- https://raw.githubusercontent.com/git/git/master/Documentation/RelNotes/2.56.0.adoc
How we research: where our facts come from, how often we re-check them, and why no one can pay for a review or a verdict.