One Git command that's easy to forget about is git reflog.
Say you have a couple of commits and accidentally run:
git reset --hard HEAD~1
Now the latest commit is missing from git log.
The first thing to try is:
git reflog
You might see something like:
a81b2c3 HEAD@{0}: reset: moving to HEAD~1
f42d981 HEAD@{1}: commit: update config
That second entry is the interesting one.
Even though the commit isn't showing up in your normal log anymore, Git may still have it locally.
You can create a branch from it:
git branch recovered-work f42d981
Then check what was in that commit:
git show f42d981
Or switch to the recovered branch if that's what you need:
git switch recovered-work
I think creating a separate branch is a good idea here. It gives you somewhere to inspect the old commit without immediately changing your current branch again.
One thing to be careful about: reflog doesn't save everything.
If you changed a file but never committed it, and then ran git reset --hard, reflog generally won't bring those uncommitted edits back. It records changes to Git references, not every version of your working files.
Reflog entries also don't stay around forever, so it's better to recover the commit sooner rather than later.
Anyway, it's a useful command to remember before assuming a missing commit is permanently gone.
Top comments (0)