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.
On this page
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?
- An edit in the working tree, not committed: use
git restore. - A change in the staging area: use
git restore --staged. - A commit that only exists on my computer: use
git reset(orcommit --amendfor the last one). - A commit that is already pushed and shared: use
git revert.
The starting history:
git log --onelinebd42d4c Add feature C (broken)
e9caa00 Add feature B
2d4a26a Add feature A
615ed97 Initial apprestore: 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.
echo 'oops edit' > app.js
git status -s
git restore app.js
git status -s
cat app.js M app.js
v4 brokenAfter 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:
git restore --source=HEAD~2 app.js
cat app.js
git status -sv2
M app.jsThe 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:
git add extra.txt
git restore --staged extra.txt
git status -s?? 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.
[soft] HEAD: e9caa00 Add feature B
M app.js
M config.envapp.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.
[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.
HEAD is now at e9caa00 Add feature B
[hard] HEAD: e9caa00 Add feature BThe 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.
git revert HEAD
git log --oneline[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 appThe 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:
git revert -m 1 HEADIt 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:
git reset --hard HEAD~2
git reflog | head -4HEAD 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:
git reset --hard 'HEAD@{1}'
git log --oneline | head -2HEAD 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:
git branch -D scratch
git reflog | grep Precious
git branch rescued 7f4b236Deleted branch scratch (was 7f4b236).
7f4b236 HEAD@{1}: commit: Precious workNote 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#
- Make a repository with four commits. Edit a file, run
git restoreon it, and confirm the edit is gone. Then restore the same file from two commits earlier with--source. - Stage a new file, check
git status -s, then unstage it withgit restore --staged. - Make a note of the commit hash, then run
git reset --soft HEAD~1,git status -s, and re-commit. Repeat with--mixedand--hard, and compare the status after each. - Revert the most recent commit. Compare
git log --onelinewith what a reset would have shown. - Run
git reset --hard HEAD~2and then usegit reflogto get back to where you were.
Sign in to keep track of the lessons you finish.