Skip to content

Menu

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.

Undoing the last commitThe three reset modes differ in where the undone changes end up; revert is the safe choice on shared branches, and reflog finds commits you reset away.Undo the last commitreset--softHEAD~1changes staystagedresetHEAD~1changesunstagedreset--hardHEAD~1changesdeletedShared branch?git revert --no-edit HEAD adds a cancellingcommitReset too far?git reflog shows the old hash; reset --hard<hash>
The three reset modes differ in where the undone changes end up; revert is the safe choice on shared branches, and reflog finds commits you reset away.

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 feature

Move to an existing local branch, or create one tracking origin/feature if only the remote branch exists

git switch -c fix/login

Create 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.0

Check out a tag or commit without a branch, for a quick look or build

git restore app.js

Throw away unstaged edits to a file and go back to the staged or committed version

git restore --staged app.js

Unstage a file but keep your edits in the working tree

git restore --source=HEAD~2 app.js

Bring 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-edit

Add 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 .env

Stop tracking a file you committed by mistake but keep it on disk; add it to .gitignore next

git reset --soft HEAD~1

Undo the last commit but keep its changes staged, ready to commit again

git reset HEAD~1

Undo the last commit and leave its changes unstaged in the working tree

git reset --hard HEAD~1

Delete 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 HEAD

Add a new commit that cancels the last one, safe on shared branches

git reflog -10

Show 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 -vv

List local branches with their upstream and ahead/behind counts

git branch --merged main

List branches whose commits are all in main, so they are safe to delete

git branch -d old-feature

Delete a branch only if it is merged; refuses with 'not fully merged' otherwise

git branch -D old-feature

Force-delete a branch even if its commits are not merged anywhere

git branch -m new-name

Rename the current branch

git push -u origin HEAD

Publish the current branch under the same name and set it as upstream

From the docs, not run by us

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 main

Replay your branch's commits on top of the current main

git rebase -i HEAD~3

Open the last three commits in your editor to reorder, squash, reword or drop them

git commit --fixup=abc1234

Make a commit marked to be folded into commit abc1234 later

git rebase -i --autosquash main

Start an interactive rebase with fixup! commits already moved under their targets

git rebase --continue

Carry on after you fixed a conflict and staged the files

git rebase --abort

Cancel 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 -u

Stash untracked files too, not only tracked ones

git stash list

Show saved stashes as stash@{0}, stash@{1} and so on, newest first

git stash pop

Apply 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.0

Start a bisect with HEAD as bad and tag v1.4.0 as the last known good

git bisect good

Mark the checked-out commit as working; Git halves the range again

git bisect bad

Mark the checked-out commit as broken

git bisect run npm test

Let a script decide: exit 0 is good, 1 to 127 (except 125) is bad, 125 skips

From the docs, not run by us
git bisect reset

End 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 -v

Show each remote's fetch and push URLs

From the docs, not run by us
git remote set-url origin [email protected]:me/app.git

Switch a remote from HTTPS to an SSH URL

From the docs, not run by us
git fetch --prune

Download new commits and delete remote-tracking branches that were removed upstream

From the docs, not run by us
git pull --rebase

Fetch and rebase your local commits on top instead of making a merge commit

From the docs, not run by us
git push --force-with-lease

Overwrite the remote branch only if nobody pushed since your last fetch

From the docs, not run by us
git log --oneline --graph --all

Draw every branch as a compact text graph

git log --oneline main..feature

List commits on feature that are not on main yet

git log -S "apiKey" --oneline

Find commits that added or removed the string apiKey

git log -p --follow -- src/app.js

Show every change to one file, following it across renames

git diff --staged

Show what will go into the next commit

git diff --stat HEAD~2

Summarise files and line counts changed in the last two commits

git blame -L 40,60 src/app.js

Show 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 --hard with uncommitted work you wanted to keep.

    Reset only touches committed history safely; stash or commit first. If the work was committed, git reflog shows the old hash and git reset --hard <hash> brings it back.

  • Rebasing or amending a branch that teammates already pulled, then force-pushing.

    Use git revert on 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 name and getting a file reverted when you meant a branch, or the other way round.

    Use git switch for branches and git restore for files. When you do use checkout for files, put -- before the path.

  • Forgetting git bisect reset and then committing on a detached HEAD.

    Always end with git bisect reset. If you already committed there, create a branch on it with git switch -c rescue before switching away.

  • Expecting git stash to 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 -a if you also need ignored files.