Linux and Git commands · Lesson 19 of 23

Undoing things: restore, reset, revert and reflog

Pick the right undo for each situation, understand soft, mixed and hard reset, use revert on shared history, and recover lost commits with the reflog.

6 min readFree

On this page
  1. Why this matters
  2. Choose by the question you are asking
  3. restore: undo uncommitted changes
  4. reset: move the branch backwards
  5. revert: undo by adding a new commit
  6. The reflog: your safety net
  7. A habit that makes undo cheap
  8. Try it

Everyone makes mistakes in Git: a bad edit, a wrong commit, a commit that broke production. Git has several ways to undo, and they are not interchangeable. This lesson gives you a way to choose, and a safety net for the day you undo too much.

Why this matters#

When something goes wrong at work, you are usually under time pressure, and the commands that look similar do very different things. git reset --hard can destroy hours of work; git revert can fix a broken release safely in a minute. Knowing which is which, and knowing that the reflog can usually save you, lets you act quickly without fear.

All outputs here come from a practice repository with four commits, the last of which, "Add feature C (broken)", we want to deal with.

Choose by the question you are asking#

Ask first: where is the thing I want to undo?

  1. An edit in the working tree, not committed: use git restore.
  2. A change in the staging area: use git restore --staged.
  3. A commit that only exists on my computer: use git reset (or commit --amend for the last one).
  4. A commit that is already pushed and shared: use git revert.

The starting history:

Shell
git log --oneline
Text
bd42d4c Add feature C (broken)
e9caa00 Add feature B
2d4a26a Add feature A
615ed97 Initial app

restore: undo uncommitted changes#

git restore <file> puts a file back to the version in the staging area, or the last commit if nothing is staged. It overwrites your working copy.

Shell
echo 'oops edit' > app.js
git status -s
git restore app.js
git status -s
cat app.js
Text
 M app.js
v4 broken

After the restore, status is empty and the file has its committed content. This cannot be undone: the edit was never committed, so Git has no copy.

You can restore a file from any commit with --source:

Shell
git restore --source=HEAD~2 app.js
cat app.js
git status -s
Text
v2
 M app.js

The file now has the content from two commits ago and shows up as modified, ready for you to commit. To unstage something you added by mistake without touching the file:

Shell
git add extra.txt
git restore --staged extra.txt
git status -s
Text
?? extra.txt

(Older guides use git checkout -- file and git reset HEAD file for these two; the newer commands are clearer.)

reset: move the branch backwards#

git reset <commit> moves the current branch to another commit, usually an earlier one, discarding the later commits from the branch. It has three modes that differ in what they do with the staging area and your files. We ran each from the same start: a repository on commit C, with an extra uncommitted edit in config.env, resetting to HEAD~1.

--soft moves the branch only. Your files and the staging area are untouched, so everything from the removed commit is still staged and ready to commit again.

Text
[soft] HEAD: e9caa00 Add feature B
M  app.js
 M config.env

app.js shows M in the first column: the change from commit C is now staged. Use this to redo a commit, for example to squash the last few commits into one.

--mixed is the default. It moves the branch and clears the staging area, but leaves your files alone, so the changes are still in the working tree and unstaged.

Text
[mixed] HEAD: e9caa00 Add feature B
 M app.js
 M config.env

--hard moves the branch, clears the staging area and overwrites your working tree to match.

Text
HEAD is now at e9caa00 Add feature B
[hard] HEAD: e9caa00 Add feature B

The status after --hard was empty, and app.js held the older v3. Our uncommitted edit to config.env was gone too. --hard is the one that destroys uncommitted work. Check git status before you use it.

The summary in words: soft keeps your changes staged, mixed keeps them unstaged, hard throws them away.

git reset takes any commit reference, such as HEAD~3, a hash, a branch name or origin/main. A common use is git reset --hard origin/main, which makes your local branch exactly match the remote, throwing away local commits and edits.

revert: undo by adding a new commit#

git revert <commit> does not remove history. It creates a new commit that applies the opposite of the chosen commit.

Shell
git revert HEAD
git log --oneline
Text
[main 7cd385a] Revert "Add feature C (broken)"
 1 file changed, 1 insertion(+), 1 deletion(-)
7cd385a Revert "Add feature C (broken)"
bd42d4c Add feature C (broken)
e9caa00 Add feature B
2d4a26a Add feature A
615ed97 Initial app

The broken commit is still in history, followed by the commit that cancels it. The file is back to v3. Because it only adds a commit, revert is safe on a branch other people use, and everyone gets the fix with a normal pull.

You can revert any earlier commit, not only the last one, but it may conflict if later commits touched the same lines. We reverted "Add feature A" after later changes and got a conflict, shown as UU app.js in status. Resolve it as in lesson 16 and run git revert --continue, or give up with git revert --abort.

To revert a merge commit, tell Git which parent counts as the mainline, usually the first:

Shell
git revert -m 1 HEAD

It removed the merged branch's files in our test. One gotcha: afterwards, Git considers the branch merged already, so merging the same branch again brings nothing back unless you first revert the revert. Remember this if you ever have to revert a merge.

The trade-off: A bad commit is on main and has been pushed. Teammates have already pulled it. How do you remove its effect?

  • git revert: Adds a new commit that cancels the bad one. History is preserved, nobody's copy breaks, and it works with normal pull and push. The bad commit and its fix both stay visible, and reverting a merge needs extra care.
  • git reset --hard and force-push: Makes the history look as if the commit never happened, which is tidy. But everyone who pulled the bad commit now has a diverged history and must repair it, and a force-push can overwrite teammates' new work.

For a commit that is only on your machine, reset is the right tool because nobody else has to cope with the difference.

The reflog: your safety net#

Every time HEAD moves, Git records it in the reflog. Even after a hard reset, the old commits still exist in the repository for a while, usually at least 30 days for those no longer referenced by any branch. Here we hard-reset two commits back and then looked at where HEAD had been:

Shell
git reset --hard HEAD~2
git reflog | head -4
Text
HEAD is now at bd42d4c Add feature C (broken)
bd42d4c HEAD@{0}: reset: moving to HEAD~2
f592a8d HEAD@{1}: reset: moving to f592a8db8e650b4277669c6441a39cfc82d32716
f592a8d HEAD@{2}: commit: Add feature D
7cd385a HEAD@{3}: revert: Revert "Add feature C (broken)"

HEAD@{2} shows the commit "Add feature D" from before the reset. Go back to it:

Shell
git reset --hard 'HEAD@{1}'
git log --oneline | head -2
Text
HEAD is now at f592a8d Add feature D
f592a8d Add feature D
7cd385a Revert "Add feature C (broken)"

The commits are back. The reflog is local to your machine and per repository, and it does not help for changes that were never committed: reset --hard on uncommitted edits is truly final.

The same trick rescues a deleted branch. We committed "Precious work" on a branch, deleted the branch with git branch -D, and found the commit again in the reflog:

Shell
git branch -D scratch
git reflog | grep Precious
git branch rescued 7f4b236
Text
Deleted branch scratch (was 7f4b236).
7f4b236 HEAD@{1}: commit: Precious work

Note that Git printed the hash when it deleted the branch. Copying that number is already enough. Last-resort tools exist too, for example git fsck --lost-found lists dangling objects that nothing points to, but the reflog is nearly always the right place to look first.

A habit that makes undo cheap#

Commit often, even work in progress. A commit is a restore point you can always return to, and you can tidy the messy ones with an interactive rebase (lesson 18) before sharing. Before a risky operation, create a backup branch with git branch backup. It costs nothing.

Try it#

  1. Make a repository with four commits. Edit a file, run git restore on it, and confirm the edit is gone. Then restore the same file from two commits earlier with --source.
  2. Stage a new file, check git status -s, then unstage it with git restore --staged.
  3. Make a note of the commit hash, then run git reset --soft HEAD~1, git status -s, and re-commit. Repeat with --mixed and --hard, and compare the status after each.
  4. Revert the most recent commit. Compare git log --oneline with what a reset would have shown.
  5. Run git reset --hard HEAD~2 and then use git reflog to get back to where you were.