Looking back and undoing · lesson 7 of 7
Commands and ideas in this lesson
Agit reflogeverywhere HEAD has been
BHEAD@{1}where HEAD was one move ago
CGarbage collectionwhen Git finally clears old commits

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
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

Git sandbox
  1. Find the lost commits with git reflog
  2. 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

  1. 67e435d Cover the leche flan with foil
  2. ba04ca2 Add lumpiang shanghai recipe
  3. 98a5edc Add a script that builds the index page
  4. d078487 HEAD →main Make the sinigang steps clearer
  5. 145dc1e Add pancit bihon recipe
  6. 52d1f4d Say how much salt goes in the adobo
  7. 1c17fad Add a README
  8. 9c53888 Add leche flan recipe
  9. 1f359a2 Add sinigang recipe
  10. 601c654 Add 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.

Git sandbox
  1. Find the original commit in git reflog
  2. Move main back to it, keeping the README change: git reset --soft HEAD@{1}
  3. 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

  1. 3f3791d HEAD →main Ask before adding recipes
  2. 67e435d Cover the leche flan with foil
  3. ba04ca2 Add lumpiang shanghai recipe
  4. 98a5edc Add a script that builds the index page
  5. d078487 Make the sinigang steps clearer
  6. 145dc1e Add pancit bihon recipe
  7. 52d1f4d Say how much salt goes in the adobo
  8. 1c17fad Add a README
  9. 9c53888 Add leche flan recipe
  10. 1f359a2 Add sinigang recipe
  11. 601c654 Add 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

Git sandbox
  1. Look through git reflog for 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.

Spot the trapAna

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

What does HEAD@{1} mean?
Which of these can the reflog bring back?
About how long can you normally recover a commit that’s no longer on any branch?

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.

All sources for Simple Git