A ladder of undo tools, from gentle to sharp, and why committed work almost never dies.
Everyone breaks something eventually. You will too, and that is fine. Here is the most comforting fact in all of git: once a photo is in the album, it is almost impossible to truly lose. Git hoards everything. Even photos you think you threw away usually still exist in the album's back pocket.
The genuinely risky moments all happen before the photo is taken. Edits sitting in a file that was never committed have no photo to fall back on. Keep that in mind as we climb the undo ladder, starting with the gentlest tool and ending at the sharp drawer you should barely touch.
You took a photo two minutes ago, and the sticky note says "tdoo update". Or one file missed the basket. No need for a second, apologetic photo. You can simply retake the last one.
git commit --amend swaps your newest commit for a fresh one. Forgot a file? Put it in the basket with git add first, then amend. The new photo includes everything in the basket plus the old photo's contents.Here is what it looks like with your own latest save point. Imagine the note had come out wrong:
# the newest photo in your album, with a typo on the sticky note $ git log --oneline -1 f30f6b7 tdoo update $ git commit --amend -m "todo update" [main 9c1e4d2] todo update $ git log --oneline -1 9c1e4d2 todo update # same work, retaken photo, brand new receipt number
In your playground folder, make a typo on purpose, then fix it with amend:
cd /tmp/git-playground echo "undo practice" >> notes.txt git add notes.txt git commit -m "tdoo update" # typo on purpose git commit --amend -m "todo update" git log --oneline -1 # one tidy photo, new receipt number
You opened notes.txt, experimented bravely, and now it is a mess. You have not taken a photo since the damage. You just want the file back the way it was.
git restore notes.txt puts the file back exactly the way the most recent photo shows it. Your messy edits vanish, and the photographed version returns.$ git restore notes.txt $ git status nothing to commit, working tree clean
git restore discards them forever. That is the whole moral of this lesson: commit often. Photos are free, and anything photographed is nearly unlosable.Ruin the file on purpose, then watch the last photo bring it back:
echo "total garbage" > notes.txt # ruin it cat notes.txt git restore notes.txt cat notes.txt # back to the last photo
git revert <sha> takes a NEW photo that is the exact opposite of the bad one. If the bad commit added a line, the revert removes it. History only grows forward, nothing is erased, and nobody's album ever disagrees with anybody else's.$ git revert b4dc0de [main e7a9f12] Revert "oops" 1 file changed, 1 deletion(-) # a new photo that cancels the bad one
Notice what the safe way does not do: C is still in the chain. The album simply gained one more bead, and anyone who syncs gets D. The problem is gone, the story is honest, and nobody is confused.
One day you will switch branches, undo something, switch again, and look up with no idea where your work went. Git keeps a private diary for exactly this moment.
git reflog lists every place your "you are here" marker has been recently, even moves caused by mistakes. Each line comes with a receipt number you can jump back to.$ git reflog 9c1e4d2 HEAD@{0}: commit (amend): todo update f30f6b7 HEAD@{1}: commit: tdoo update db039fb HEAD@{2}: commit: review fixes 836f2de HEAD@{3}: checkout: moving from launch-plan to main fb9fecb HEAD@{4}: commit: launch tracking # every line is a place you have been, with its receipt number. # even the typo photo you amended away in section 2 is still here!
Find the line from back when life was good, then rescue that spot onto a fresh bookmark:
$ git switch -c rescue 836f2de Switched to a new branch 'rescue'
Older guides write git checkout -b rescue 836f2de. Checkout is the older word for the same move. Put the reflog together with the fact that commits are almost unlosable, and the conclusion is calming: a wrong turn is nearly always recoverable.
Two commands live at the bottom of the ladder. You will meet them in other people's advice long before you need them.
git reset --hard and git push --force have legitimate uses, and they are the two commands that can destroy work even after the photo was taken, yours or a teammate's. Beginner rule: if a command needs --hard or --force, stop and double-check with someone first. There is almost always a gentler rung on the ladder that does the job.| Situation | Tool |
|---|---|
| Typo in the last commit message, or forgot a file | git commit --amend |
| Ruined a file, never committed the mess | git restore <file> |
| A bad commit is already shared | git revert <sha> |
| "Where was I before this mess?" | git reflog |
Q. A bad commit was pushed and your teammates already pulled it. What is the safest fix?
Why is revert safer than erasing the commit?