Skip to the content

Lesson 97 of 296

Undoing changes: restore, reset, revert, reflog and bisect

What you will learn

  • The right command for every "undo" situation
  • reset and its three modes
  • revert for shared history
  • reflog: rescuing "lost" commits
  • bisect: finding which commit introduced a bug
On this page

Which undo command?

SituationCommand
Discard unstaged changes to a filegit restore file.js
Unstage a file, keep the changesgit restore --staged file.js
Fix the last commit (not pushed)git commit --amend
Undo local commits, keep the changesgit reset --soft HEAD~1
Undo local commits and throw changes awaygit reset --hard HEAD~1
Undo a commit that is already pushedgit revert <hash>
Find a commit you "lost"git reflog
Find which commit caused a buggit bisect

HEAD~1 means "one commit before HEAD". HEAD~3 means three commits back.

reset

git reset moves the current branch back to an earlier commit. The three modes decide what happens to the changes from the removed commits:

ModeBranch moves backStaging areaYour files
--softYesChanges stay stagedUnchanged
--mixed (the default)YesChanges unstagedUnchanged
--hardYesClearedChanges deleted

Examples:

git reset --soft HEAD~1        "undo the last commit, I want to redo the commit"
git reset HEAD~2               "undo two commits, keep all changes as unstaged edits"
git reset --hard HEAD~1        "throw away the last commit completely"
git reset --hard origin/main   "make my branch exactly match the remote"

--hard deletes uncommitted work permanently. Run git status first.

Only reset commits that are not pushed. Resetting rewrites history, like rebase (Lesson 96).

revert: the safe undo for shared history

git revert creates a new commit that does the opposite of an earlier commit. History is not rewritten, so it is safe on main and shared branches.

git revert a3f9c21

If commit a3f9c21 added a line, the revert commit removes it.

This is how teams quickly undo a bad change that is already deployed: revert, push, deploy.

resetrevert
How it undoesMoves the branch back; commits disappear from the branchAdds a new commit that cancels the old one
Rewrites historyYesNo
Safe on shared branchesNoYes

Reverting a merge commit needs an extra option (-m 1) to say which parent to keep.

reflog: your safety net

Git records every movement of HEAD (commits, resets, rebases, switches) in the reflog, normally for about 90 days. So commits that seem "lost" after a bad reset or rebase can almost always be recovered.

git reflog
e4b1d02 HEAD@{0}: reset: moving to HEAD~3
9c7a6f1 HEAD@{1}: commit: Add payment webhook
...

To recover:

git reset --hard 9c7a6f1

or create a branch there:

git switch -c rescue 9c7a6f1

The reflog is local: it only exists on your machine. Knowing it exists is what lets you stay calm when Git goes wrong.

bisect: find the commit that broke something

"It worked last week. Now it is broken. There are 200 commits in between." Checking each commit would take hours. git bisect uses binary search (Part 10): it checks the commit in the middle, then halves the range each time. 200 commits need only about 8 checks.

git bisect start
git bisect bad                  the current commit is broken
git bisect good v2.3.0          this tag (or commit hash) was working

Git checks out a commit in the middle. Test it, then tell Git:

git bisect good                 this one works
git bisect bad                  this one is broken

Repeat until Git prints: <hash> is the first bad commit. Then:

git bisect reset                go back to where you started

Automatic bisect: if a command can test for the bug (exit code 0 means good, anything else means bad), Git can run the whole search for you:

git bisect start HEAD v2.3.0
git bisect run npm test

Common mistakes

  • Using reset on pushed commits instead of revert.
  • reset --hard with uncommitted work you wanted.
  • Thinking a commit is gone forever without checking git reflog.
  • Forgetting git bisect reset at the end.

How to say it in an interview

"For local, unpushed work I use restore to discard file changes, and reset to move the branch back: soft keeps changes staged, mixed keeps them unstaged, and hard deletes them. For anything already pushed, I use git revert, which adds a new commit that undoes the old one without rewriting history. If I lose commits after a bad reset or rebase, git reflog shows every position HEAD has been, so I can reset or branch back to it. To find which commit introduced a bug, I use git bisect, which binary-searches the history, and bisect run can automate it with a test command."

Key words

WordMeaning
restoreDiscard or unstage changes to files
resetMove the branch back to an earlier commit
revertAdd a commit that undoes an earlier one
reflogA local record of every position HEAD has been
bisectBinary search through history to find a bad commit
HEAD~1The commit before HEAD

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.