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?
| Situation | Command |
|---|---|
| Discard unstaged changes to a file | git restore file.js |
| Unstage a file, keep the changes | git restore --staged file.js |
| Fix the last commit (not pushed) | git commit --amend |
| Undo local commits, keep the changes | git reset --soft HEAD~1 |
| Undo local commits and throw changes away | git reset --hard HEAD~1 |
| Undo a commit that is already pushed | git revert <hash> |
| Find a commit you "lost" | git reflog |
| Find which commit caused a bug | git 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:
| Mode | Branch moves back | Staging area | Your files |
|---|---|---|---|
--soft | Yes | Changes stay staged | Unchanged |
--mixed (the default) | Yes | Changes unstaged | Unchanged |
--hard | Yes | Cleared | Changes 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 a3f9c21If 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.
| reset | revert | |
|---|---|---|
| How it undoes | Moves the branch back; commits disappear from the branch | Adds a new commit that cancels the old one |
| Rewrites history | Yes | No |
| Safe on shared branches | No | Yes |
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 refloge4b1d02 HEAD@{0}: reset: moving to HEAD~3
9c7a6f1 HEAD@{1}: commit: Add payment webhook
...To recover:
git reset --hard 9c7a6f1or create a branch there:
git switch -c rescue 9c7a6f1The 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 workingGit checks out a commit in the middle. Test it, then tell Git:
git bisect good this one works
git bisect bad this one is brokenRepeat until Git prints: <hash> is the first bad commit. Then:
git bisect reset go back to where you startedAutomatic 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 testCommon mistakes
- Using
reseton pushed commits instead ofrevert. reset --hardwith uncommitted work you wanted.- Thinking a commit is gone forever without checking
git reflog. - Forgetting
git bisect resetat 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
| Word | Meaning |
|---|---|
| restore | Discard or unstage changes to files |
| reset | Move the branch back to an earlier commit |
| revert | Add a commit that undoes an earlier one |
| reflog | A local record of every position HEAD has been |
| bisect | Binary search through history to find a bad commit |
| HEAD~1 | The commit before HEAD |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.