Reflog & Recovery — Git

git reflog records every HEAD move (HEAD is the marker of where you are now) on your laptop, and the newest is HEAD@{0}. A commit that vanished from git log…

You type git reset --hard HEAD~1, press Enter, and the commit you worked on all day disappears from git log. You start to panic. Stay calm: that commit is almost certainly still there, and Git knows where.

git reflog is a record of every time HEAD (the marker of where you are now) moved on your laptop: each commit, branch switch, reset or merge. A commit that "vanished" from git log is usually still there, and the reflog tells you its hash. It's like the location history in Google Maps. Even if you forget which road you took, your phone recorded every place you've been, so you can go back there. In Git, those places are the commits HEAD has been on, and the record on your phone is the reflog.

The "lost" commit

This is what happens, in a new repo with one file, app.js. The commands below make two commits, then run the wrong reset:

git init
git add .
git commit -m "start"
echo added >> app.js
git add .
git commit -m "important feature"
git reset --hard HEAD~1     # oops, wrong command

echo added >> app.js adds the line added to the end of app.js. After that reset, git log shows only one commit:

$ git log --oneline
9a8b7c6 (HEAD -> main) start

important feature no longer appears, because git log only lists commits that can be reached from a branch. That means the commit a branch points at, then its parent (the commit before it), and so on, back to the first commit. But the commit hasn't been deleted. Git still keeps it, there's just no branch pointing at it. The hashes here match the Lab, but on your laptop they'll be different.

Reading git reflog

This is the reflog after that reset:

$ git reflog
9a8b7c6 (HEAD -> main) HEAD@{0}: reset: moving to HEAD~1
3d4e5f6 HEAD@{1}: commit: important feature
9a8b7c6 (HEAD -> main) HEAD@{2}: commit (initial): start

Recovering with HEAD@{1}

To move main back to where it was before the reset, run reset again to HEAD@{1}:

$ git reset --hard HEAD@{1}
HEAD is now at 3d4e5f6 important feature

$ git log --oneline
3d4e5f6 (HEAD -> main) important feature
9a8b7c6 start

A deleted branch

The experiment branch holds a commit, 7c1d2e9 try discount, that was never merged, and then the branch gets deleted:

$ git branch -D experiment
Deleted branch experiment (was 7c1d2e9).

A branch is only a name pointing at a commit, so only the name is gone. The commit's hash is in that message, and in the reflog: 7c1d2e9 HEAD@{1}: commit: try discount. Make the branch name again at that commit:

git branch experiment 7c1d2e9     # a new branch called experiment, at commit 7c1d2e9

HEAD~1 is not HEAD@{1}

Written Means After the reset --hard HEAD~1 above
HEAD~1 one commit before HEAD in the history (its parent) doesn't exist, because start is the first commit
HEAD@{1} where HEAD was one move ago 3d4e5f6 important feature

~ goes back in the history, from a commit to its parent. @{} goes back in time, from one HEAD move to the move before it. To recover, use HEAD@{1}.

How long the reflog is kept

⚠️ So the recovery doesn't go wrong

TL;DR: git reflog records every HEAD move (HEAD is the marker of where you are now) on your laptop, and the newest is HEAD@{0}. A commit that vanished from git log after reset --hard is still there: find its hash in the reflog, then run git reset --hard HEAD@{1} or git reset --hard <hash>. A deleted branch comes back with git branch <name> <hash>. HEAD~1 is the parent (the commit before) in the history, while HEAD@{1} is where HEAD was before the last move. The reflog is local, its entries are kept for 90 days (30 days for commits that are no longer in any branch's history), and it can't save changes that were never committed.

⚡ The "lost" commit

Guess first: after git reset --hard HEAD~1, your last commit doesn't show in git log. Is it deleted?

Not yet. Git still keeps it, there's just no branch pointing at it.

Meaning: git log only shows commits that can be reached from a branch: the commit a branch points at, and every commit before it.

⚡ Look in the reflog

Write:

git reflog

Meaning: The reflog records every time HEAD (the marker of where you are now) moves on your laptop, newest at the top. This is where you find the lost commit's hash. HEAD@{0} is where you are now, HEAD@{1} one move before.

⚡ Go back to where you were

Write:

git reset --hard HEAD@{1}

Meaning: This command moves the branch back to where HEAD was before the last move, so the lost commit is back. HEAD@{1} steps back in time, while HEAD~1 steps back in the history (to the commit before). To recover, use HEAD@{1}. A deleted branch can come back too, with its hash from the reflog: git branch experiment 7c1d2e9 makes the experiment branch again at the commit 7c1d2e9.

⚡ The reflog isn't a backup

Meaning: The reflog is not a backup copy. It lives only on your laptop, its entries expire, and it only records commits. Changes that were never committed can't be saved.

What you will learn