What you can do after this lesson
You will have a safe way to undo shared work and a safety net for the times you think you lost something. The two tools in this lesson are opposites in a useful way: revert is a polite undo that anyone can accept, and reflog is your private black box recorder that finds work after a mistake.
git revert: undo by adding
Imagine a commit has already been pushed, and a teammate has pulled it. Using reset to delete it would give them a history that disagrees with yours. revert avoids that by adding a new commit that does the opposite of the old one:
git revert HEAD --no-edit
git log --oneline
cat shopping.txt[main c2edc15] Revert "Add salt"
Date: Tue Nov 10 10:00:00 2026 +0000
1 file changed, 1 deletion(-)
c2edc15 Revert "Add salt"
aa986bd Add salt
f2afcbf Add yeast
4c52fa5 Add shopping list
buy flour
buy yeastThis is example output; your hashes will differ. Notice that Add salt is still in history. The new commit removes its effect. That is exactly what makes revert safe to push: it only adds to history, so everyone's copy can simply fetch it.
Without --no-edit, Git opens your editor with a default message such as Revert "Add salt". You can improve it with a reason, which future readers will appreciate.
Reverting an older commit
You can revert any commit, not just the latest. git revert HEAD~2 reverses the effect of the commit two steps back, creating a new commit at the tip. If later commits changed the same lines, Git reports a conflict and asks you to resolve it, exactly like a merge conflict (the next section of the course). To abandon an in-progress revert run git revert --abort.
Revert, reset or restore?
| You want to | Use |
|---|---|
| Undo a commit already shared with others | git revert |
| Undo local commits nobody has | git reset |
| Discard edits not yet committed | git restore |
git reflog: the safety net
Every time HEAD moves, whether by committing, resetting, switching branches or rebasing, Git writes a line to a local log called the reflog. It is not the same as git log. The log follows parent links from the current commit. The reflog follows where you have been, so it still lists commits that nothing points to any more.
git reflogaa986bd HEAD@{0}: commit: Add salt
f2afcbf HEAD@{1}: commit: Add yeast
4c52fa5 HEAD@{2}: commit (initial): Add shopping listHEAD@{0} is the latest movement. Each entry has the hash, a position and a description. You can use the position in commands: HEAD@{1} means where HEAD was one move ago.
Rescuing a deleted branch
Create a feature branch with one commit, return to main, then delete the branch forcefully:
git branch -D feature
git reflog -4Deleted branch feature (was 8d12f1e).
aa986bd HEAD@{0}: checkout: moving from feature to main
8d12f1e HEAD@{1}: commit: Feature work
aa986bd HEAD@{2}: checkout: moving from main to feature
aa986bd HEAD@{3}: commit: Add saltThe commit 8d12f1e is still stored. Git simply removed the label. Give it a new label:
git branch feature-rescued HEAD@{1}
git log --oneline feature-rescued -28d12f1e Feature work
aa986bd Add saltThe branch is back, with its commit. Notice that git branch -D even printed the old hash when deleting. Noting that hash is itself a rescue plan.
Rescuing after a hard reset
The previous lesson reset a branch with --hard HEAD~1. The commit that disappeared is the entry just before the reset line in the reflog. git reset --hard HEAD@{1} moves the branch straight back, and git branch rescue <hash> saves it under a name without moving anything, which is the more cautious choice when you are unsure.
Limits of the reflog
- It is local. It is not pushed, and a fresh clone has an empty one.
- Entries expire. The Git manual (git help config) says reflog entries expire after 90 days by default, and entries for commits no longer reachable from any branch after 30 days, after which cleanup may delete the underlying commits. Do not rely on those windows for anything important; rescue promptly.
- It cannot bring back uncommitted work. A file you never staged or committed was never recorded.
Common mistakes
- Using reset on shared commits instead of revert. Others will have copies that no longer match.
- Forgetting revert leaves the old commit in history. This is intended.
- Panicking after a mistake and running more commands. Look at
git reflogfirst, calmly. Most mistakes are recoverable. - Expecting reflog to rescue unsaved edits. It only knows commits and positions.
- Reverting a merge commit without telling Git which parent to keep. Merge reverts need
-m 1and extra care; look up the manual before attempting one.
Exercise
- Make three commits in a practice repository.
- Revert the middle one. Does it conflict? Check the log and file contents.
- Create a branch, commit on it, switch away and force-delete it.
- Rescue it with the reflog.
Show one possible answer
git revert HEAD~1 --no-edit
git log --oneline
git switch -c temp
echo x > t.txt && git add t.txt && git commit -m "Temp work"
git switch main
git branch -D temp
git reflog -4
git branch temp-rescued HEAD@{1}Whether the revert conflicts depends on whether the later commit touched the same lines. If it does, resolve the file, git add it, and run git revert --continue.
Key points
- git revert <commit> creates a new commit that reverses an earlier one, leaving history intact and safe to share.
- Revert is the right undo for commits that other people may already have.
- git reflog is a private log of where HEAD and branches pointed, which is how you find work after a reset or deleted branch.
- Reflog entries are local to your clone and expire eventually, so rescue lost work soon.
- To rescue a commit, create a branch pointing at it with git branch <name> <hash>.