Sub topic 11
Getting lost commits back with the reflog
This chapter assumes you can commit, branch, and read git log. It also assumes you know that git reset --hard moves a branch and rewrites your index and working tree. You should know the three areas from earlier chapters: working tree, index, and HEAD.
What the reflog is
The reflog is a local journal. Each time HEAD or a branch tip moves, Git records the move. Commits, resets, checkouts, merges, rebases, and amends all add entries.
A "lost" commit is usually not deleted. No branch points to it, so git log no longer shows it. The commit object stays in the repository until Git's garbage collection removes it. By default, unreachable commits are kept for at least 30 days, and reflog entries for unreachable commits expire after 30 days. Entries for reachable commits last 90 days.
Two limits matter:
- The reflog is local. It is not pushed or cloned, so you cannot use a teammate's reflog.
- It records committed states only. Edits that were never committed or staged are not in it.
Worked example: undoing a hard reset
Suppose you reset three commits away by mistake.
git reset --hard HEAD~3
git reflog
git reset --hard HEAD@{1}git reflog prints one line per movement of HEAD, newest first. The output looks like this:
1f2e3d4 HEAD@{0}: reset: moving to HEAD~3
9a8b7c6 HEAD@{1}: commit: Add export feature
5d4c3b2 HEAD@{2}: commit: Fix date parsingHEAD@{0} is where HEAD is now. HEAD@{1} is where it was one move ago, which is the tip before the reset. Running git reset --hard HEAD@{1} moves the branch back to that commit.
Prefer a new branch when unsure
Resetting to a reflog entry is fine when you want to rewind exactly. If you are unsure which entry is right, create a branch at the entry instead. It changes nothing else.
git branch rescue 9a8b7c6
git log --oneline rescueYou can inspect rescue, then merge it or delete it. Use git switch rescue to work on it.
Reading entries
Every entry has a hash, a selector, and a description. The description tells you what kind of move it was.
| Description starts with | Meaning |
|---|---|
| `commit:` | A new commit was made |
| `commit (amend):` | The previous commit was replaced |
| `reset:` | A reset moved the branch |
| `checkout:` | You switched branches or detached HEAD |
| `rebase (finish):` | A rebase ended and moved the branch |
To see a single branch's history, run git reflog show my-feature. You can also use a time selector, such as main@{yesterday}, or HEAD@{2.hours.ago}.
Common errors
- Looking in the wrong place. If the work was on another branch, check
git reflog show <branch>. Plaingit reflogshows HEAD only. - Expecting uncommitted work. A
git reset --harddiscards unstaged edits for good. Only committed work comes back. - Resetting to the wrong entry.
HEAD@{1}changes after every move. Read the list again after each command, or use the commit hash. - Waiting too long. After expiry and
git gc, the commit may be gone. - Recovering in a dirty tree.
reset --hardoverwrites uncommitted edits. Commit or stash first, or use a rescue branch.
Recovering other lost states
The same method works for other mistakes. After a bad amend, the old commit appears as the entry before commit (amend):. After a rebase, find the entry just before the rebase began and reset or branch from it. After deleting a branch, find the last checkout: or commit: entry for it and run git branch <name> <hash>.
Exercise
You ran git reset --hard HEAD~2 and then git commit for a new change before realizing the reset was a mistake. Why is git reset --hard HEAD@{1} the wrong recovery command, and what should you do instead?
Show answer
After the reset, the new commit added another reflog entry, so HEAD@{1} now points to the state after the reset, not the original tip. Run git reflog, find the entry from before the reset (the commit: line for the old tip), and create a branch at its hash with git branch rescue <hash>. Merge or cherry-pick as needed.
Exercise
A colleague says their commits vanished after a hard reset on their laptop. Can you recover them by running git reflog on your machine?
Show answer
No. The reflog is local to each clone and is not shared. They must run git reflog in their own repository.