The safety net: reflog
Last updated
Git remembers every commit your HEAD has pointed at, even ones no branch reaches any more. That list, the reflog, can rescue almost any committed work after a bad reset or amend. In this lesson you'll rescue two disasters, and see the one kind of loss it can't help with.
By the end of this lesson you can
- Read git reflog and name an earlier state with HEAD@{n}
- Recover from a bad reset or amend
- Say how long Git keeps commits nothing points at
- Know that the reflog can't recover changes that were never committed
Git remembers where HEAD has been
Every time HEAD moves (a commit, an amend, a reset, a switch), Git writes a line in the reflog: the commit HEAD now points at, and what moved it. git reflog lists them, newest first:
git reflog d078487 (HEAD -> main) HEAD@{0}: reset: moving to HEAD~3 67e435d HEAD@{1}: commit: Cover the leche flan with foil ba04ca2 HEAD@{2}: commit: Add lumpiang shanghai recipe 98a5edc HEAD@{3}: commit: Add a script that builds the index page d078487 HEAD@{4}: commit: Make the sinigang steps clearer
HEAD@{0} is where HEAD is now, HEAD@{1} where it was one move ago, and so on. You can use those names anywhere you'd use a hash.
The top line says Ana reset three commits back. The line below it is where she was just before: 67e435d, with all three commits. That commit is still in the repository. No branch points at it, which is why the graph draws it dashed and git log doesn't show it, but Git hasn't thrown it away.
To go back, point the branch at it again: git reset --hard HEAD@{1} (or the hash). Check git status first: --hard also throws away uncommitted changes.
Rescue 1: a reset too far
- Find the lost commits with
git reflog - Put main back on “Cover the leche flan with foil”, with all ten commits
Working folder
no changes
Staging area
nothing staged
Repository
7 commits on main
Ana meant to undo nothing at all. She typed git reset --hard HEAD~3, and the last three commits vanished from main.
Commit graph
67e435dCover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487HEAD →main Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add adobo recipe
Rescue 2: an amend by mistake
Ana meant to make a new commit for a README change, but typed git commit -a --amend -m "Ask before adding recipes". The amend replaced her last commit, "Cover the leche flan with foil", with one holding both changes under the new message.
The plan: move main back to the commit before the amend, keeping the README change staged, then commit it properly on its own. git reset --soft keeps changes staged; HEAD@{1} is where HEAD was before the amend.
- Find the original commit in
git reflog - Move main back to it, keeping the README change:
git reset --soft HEAD@{1} - Commit the README change on its own
Working folder
no changes
Staging area
nothing staged
Repository
10 commits on main
Ana meant to make a new commit, but typed --amend. Her commit “Cover the leche flan with foil” is gone, swallowed by the README change.
Commit graph
3f3791dHEAD →main Ask before adding recipes67e435dCover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add adobo recipe
How long Git keeps things
A commit nothing points at isn't kept forever. Git tidies up now and then with garbage collection (git gc, which Git runs by itself from time to time). By default:
- reflog entries for commits still on a branch are kept for 90 days (
gc.reflogExpire); - reflog entries for commits no longer on any branch are kept for 30 days (
gc.reflogExpireUnreachable); - once nothing at all refers to a commit, it's deleted after two weeks (
gc.pruneExpire).
So a commit lost by a reset can normally be found for about a month. Don't count on longer. The reflog is also only on your computer: it isn't shared when you push, and a fresh clone starts with an empty one.
Spot the trap: what the reflog can't save
- Look through
git reflogfor Ana’s lumpia note
Working folder
no changes
Staging area
nothing staged
Repository
10 commits on main
Ana typed a note into lumpia.md, never added or committed it, then ran git restore lumpia.md.
Ana typed a note into lumpia.md (“Lola Remy adds a little cheese”), never added or committed it, then ran git restore lumpia.md. She’s just learned about the reflog.
What happens if…
Check what you learned
Sources
Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.
- Git documentation: git-reflog: the reflog and the @{n} syntax
- Git documentation: git-gc: gc.reflogExpire, gc.reflogExpireUnreachable and gc.pruneExpire
- Pro Git: Data Recovery: finding lost commits with the reflog
- Git documentation: gitrevisions, @{n}: the nth prior value of a ref