Looking Back and Undoing Mistakes Safely
Read your history, then climb the undo ladder from restore to revert to reset, and learn why committed work is hard to lose.
In this lesson you will
- Look at old versions and compare commits
- Discard an unsaved change with restore
- Undo a commit safely with revert
- Use the reflog and know what reset can destroy
About 30 minutes
“I replaced the whole plan with OOPS”
Nia is tired, and while experimenting she overwrites the whole planting plan with one word. Her stomach drops. Then she remembers that the plan is in a Git repository, and her good version is safe in history. Git is full of ways to look back and to undo, and choosing the right one depends on one question: how far did the mistake get? This lesson walks up a ladder, from the safest tool to the most dangerous.
Looking back
You cannot undo what you cannot see, so begin with looking. The session below builds a three commit history, then examines it. git log --oneline lists the commits. git diff HEAD~2 HEAD --stat compares the newest commit with the one two steps earlier, and the summary says how many lines changed. And git show HEAD~2:plan.txt prints a single file as it was at that moment. The word HEAD means “the commit I am on now”, and a tilde with a number means “that many commits before it”. So HEAD~2 is two steps back.
$ mkdir garden
$ cd garden
$ git init
Initialized empty Git repository in /home/nia/garden/.git/
$ echo "Beans: row 1" > plan.txt
$ git add plan.txt
$ git commit -m "Add planting plan"
[main (root-commit) cd4328a] Add planting plan
1 file changed, 1 insertion(+)
create mode 100644 plan.txt
$ echo "Carrots: row 2" >> plan.txt
$ git commit -am "Add carrots"
[main d9d4004] Add carrots
1 file changed, 1 insertion(+)
$ echo "Peas: row 3" >> plan.txt
$ git commit -am "Add peas"
[main f12936d] Add peas
1 file changed, 1 insertion(+)
$ git log --oneline
f12936d Add peas
d9d4004 Add carrots
cd4328a Add planting plan
$ git diff HEAD~2 HEAD --stat
plan.txt | 2 ++
1 file changed, 2 insertions(+)
$ git show HEAD~2:plan.txt
Beans: row 1
$ echo "OOPS" > plan.txt
$ cat plan.txt
OOPS
$ git status -s
M plan.txt
$ git restore plan.txt
$ cat plan.txt
Beans: row 1
Carrots: row 2
Peas: row 3
$ git revert --no-edit HEAD
[main 64fa78a] Revert "Add peas"
Date: Mon Nov 2 09:17:00 2026 +0000
1 file changed, 1 deletion(-)
$ git log --oneline
64fa78a Revert "Add peas"
f12936d Add peas
d9d4004 Add carrots
cd4328a Add planting plan
$ cat plan.txt
Beans: row 1
Carrots: row 2
$ git diff --stat HEAD~2 HEAD
Rung one: throw away an unsaved change
In the session, Nia typed the word OOPS over her plan. Git status showed the file as modified, and git restore plan.txt brought the last committed version back. The file again had its 3 lines. This is the right tool when the mistake is still in your working folder. Be careful, though. Restore discards the unsaved change for good, because Git never recorded it.
Rung two: undo a commit with a new commit
Suppose the mistake was already committed, like the peas that Nia decides she cannot grow. The safe tool is git revert. It does not erase anything. Instead it adds a new commit that does the opposite of the old one. Look at the log in the session: the history now has four commits, the last one titled Revert “Add peas”. The final command compared the file with its state two commits earlier, and the empty answer means there is no difference at all. The peas are gone, and the story of how they came and went is kept. Because revert only adds to history, it is the correct choice for anything you have already shared with other people.
Rung three: move the branch back, and the safety net
The most powerful tool is git reset --hard. It moves your branch back to an earlier commit and makes the working folder match, and commits after that point stop appearing in the log. It is the right tool for work that only exists on your computer, and the wrong tool for anything already shared. It is also where beginners are afraid, so look at the next session. It resets one step back, and then uses git reflog to get the lost commit back.
$ git reset --hard HEAD~1
HEAD is now at f12936d Add peas
$ git log --oneline
f12936d Add peas
d9d4004 Add carrots
cd4328a Add planting plan
$ git reflog
f12936d HEAD@{0}: reset: moving to HEAD~1
64fa78a HEAD@{1}: revert: Revert "Add peas"
f12936d HEAD@{2}: commit: Add peas
d9d4004 HEAD@{3}: commit: Add carrots
cd4328a HEAD@{4}: commit (initial): Add planting plan
$ git reset --hard HEAD@{1}
HEAD is now at 64fa78a Revert "Add peas"
$ git log --oneline
64fa78a Revert "Add peas"
f12936d Add peas
d9d4004 Add carrots
cd4328a Add planting plan
$ echo "an idea I never committed" >> plan.txt
$ git reset --hard
HEAD is now at 64fa78a Revert "Add peas"
$ cat plan.txt
Beans: row 1
Carrots: row 2
The reflog is Git’s private diary of every place your branch has been, and it saved the revert commit that the reset seemed to destroy. Git keeps those entries for a limited time, which by default is weeks or months, so it is a safety net and not a permanent archive. Now look at the final commands in the session. Nia wrote a new idea, never committed it, and ran git reset --hard. The idea vanished, and the reflog cannot help, because Git never saw it. That is the rule that matters most: committed work is very hard to lose, and uncommitted work is easy to lose.
| Situation | Safest tool | Does it rewrite history? |
|---|---|---|
| Unsaved change in the file | git restore file |
No |
| Bad commit, already shared | git revert |
No, it adds a commit |
| Bad commit, only on my laptop | git reset --hard |
Yes |
Running git reset --hard with unsaved changes in the working folder. They are destroyed and cannot be recovered. Commit or copy anything you care about first.
The plan, the peas and the OOPS are made up for the example. Commit IDs and times shown will differ on your computer. How long the reflog keeps entries depends on settings and Git version, so treat it as a short term safety net.
Practice for this lesson
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
What does HEAD~2 mean?
- The second branch
- Two files
- The commit two steps before the one you are on
- The second remote
The lesson compared HEAD with HEAD~2 to see how the plan grew.
Which tool throws away a change that is still only in your working folder?
- git restore file
- git revert
- git log
- git remote
Restore brought back the last committed plan after OOPS.
What does git revert do?
- Deletes the old commit
- Adds a new commit that does the opposite of an old one
- Uploads the project
- Renames the branch
The log gained a Revert commit, and the old commits stayed.
Why can the reflog not recover an idea you never committed?
- The reflog is switched off
- Reflogs only work on Mondays
- The idea was too short
- Git never recorded it, so there is nothing to find
The uncommitted line vanished after the hard reset.
Which tool is the right choice for a bad commit you have already shared?
- git reset –hard
- git revert
- Deleting .git
- Starting again
Revert only adds to history, so other people are not surprised.
Summary
- HEAD is where you are; HEAD~2 is two commits back.
- git restore discards an unsaved change for good.
- git revert undoes a commit by adding a new one.
- Committed work is very hard to lose; uncommitted work is easy to lose.
Questions
Which undo tool should a beginner use first?
Restore for unsaved changes and revert for saved ones. Both are safe. Treat reset --hard as a power tool and use it only on work that exists on your computer alone.
How long does the reflog keep entries?
It depends on your settings and your Git version. By default it is weeks or months, so recover quickly and do not rely on it as an archive.
Does revert make the old commit disappear?
No. The old commit stays in history, and the new revert commit cancels its effect. This is why it is safe for shared work.
