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
- One line for each time HEAD moved. The newest is on top.
HEAD@{0}is where you are now,HEAD@{1}is where HEAD was one move ago,HEAD@{2}two moves ago, and so on.- After the colon comes the kind of move:
commit (initial)for the first commit,commit,reset: moving to HEAD~1,checkout: moving from main to feature(also forgit switch), ormerge feature. - The hash at the front is where HEAD was after that move. So
3d4e5f6is theimportant featurecommit you're looking for. (HEAD -> main)marks the lines whose hash is wheremainis now.
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
HEAD@{1}works anywhere Git asks for a commit, just like a hash.git reset --hard 3d4e5f6does the same.--hardis safe here because there are no uncommitted changes. If there were, commit or stash them first.- The recovery is a HEAD move too, so the reflog gets a new line:
3d4e5f6 HEAD@{0}: reset: moving to HEAD@{1}. Every old number shifts by one.
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
- The reflog is local: it only exists on your laptop. It isn't sent with
git push. A fresh clone doesn't bring the reflog from another laptop: its reflog has only one line,clone: fromfollowed by the address of the original repo. - Reflog entries are kept for 90 days (the default value of the setting
gc.reflogExpire). For commits no branch reaches any more, likeimportant featureabove, the limit is shorter: 30 days (gc.reflogExpireUnreachable). After that,git gc(a clean-up of data, which also runs automatically) may throw the commit away. - The reflog only records commits. Changes that were never committed and then thrown away by
git reset --hardcan't be saved by the reflog.
⚠️ So the recovery doesn't go wrong
HEAD@{n}numbers shift. Every time HEAD moves, every number goes up by one. Readgit reflogright before you use a number, or use the hash itself.- Recovering with
HEAD~1. That goes further back in the history, not back to where you were. - Treating the reflog as a backup. It only exists on your laptop, and it expires. Push important work to a remote or mark it with a tag.
- Uncommitted work. Check
git statusbeforereset --hard. Anything never committed isn't in the reflog.
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.