Sphere
Log in Join

Recover a lost commit with git reflog after git reset --hard

I lost a day of work last Thursday. Not a metaphor, about six hours of edits to a CSV export endpoint, gone, because I ran git reset --hard HEAD~3 while on the wrong branch. The commit wasn't pushed. My first reaction was to close the laptop and pretend I'd get to it tomorrow. Then I remembered reflog exists.

## What the reset did

This was on a small Rails app, Git 2.43.0, nothing exotic. I wanted to drop three experimental commits from feature/import. I was on main. The command happily did exactly what I asked:

$ git reset --hard HEAD~3
HEAD is now at 9f2c1a4 fix: bump lodash to 4.17.21

Three commits dropped off the tip of main, and the working tree was rewound. git log --oneline -5 now showed only the older commits. No trace of my work in the normal history.

## Reflog still knew

Reflog records every movement of HEAD, including resets. It's local-only, which is why it works even for unpushed commits:

$ git reflog
9f2c1a4 (HEAD -> main) HEAD@{0}: reset: moving to HEAD~3
c47e0b9 HEAD@{1}: commit: feat: add CSV export endpoint
a13d8f0 HEAD@{2}: commit: test: cover export column mapping
72b5e11 HEAD@{3}: commit: chore: add csv gem
9f2c1a4 HEAD@{4}: checkout: moving from feature/import to main

So the work was still there as c47e0b9. I verified before touching anything:

$ git show --stat c47e0b9
commit c47e0b9a8f3d2e1b4c5d6e7f8a9b0c1d2e3f4a5b6
Author: Dana Whitfield <dana@example.com>
Date: Thu Nov 20 16:42:11 2026 +0000

feat: add CSV export endpoint

app/controllers/exports_controller.rb | 48 ++++++++++++++++++
config/routes.rb | 2 +
2 files changed, 50 insertions(+)

git show works on any commit object that still exists, even when no branch points at it. It's read-only, so there's no risk here.

## Getting it back without making a mess

Because main had already moved to where it was before those commits, I didn't want to just reset main forward, that would have rewritten a branch my coworker had checked out. I made a temp branch at the dangling commit and cherry-picked:

$ git branch rescue/csv-export c47e0b9
$ git checkout feature/import
$ git cherry-pick c47e0b9
[feature/import 3d9a1c2] feat: add CSV export endpoint
Date: Thu Nov 20 16:42:11 2026 +0000
1 file changed, 48 insertions(+)

The three original commits on the rescue branch were mine too, so I cherry-picked those after. Took five minutes once I stopped panicking.

## The bits worth knowing

Reflog entries expire. Default is 90 days for reachable commits and 30 for unreachable ones, controlled by gc.reflogExpire and gc.reflogExpireUnreachable:

$ git config --get gc.reflogExpire
90.days
$ git config --get gc.reflogExpireUnreachable
30.days

If reflog itself is gone, git fsck --no-reflogs --unreachable can still find dangling commits by object hash, though you'll be digging through output.

Two things I changed after this: I now use git switch and glance at the branch name before any destructive command, and I set up a pre-reset alias that prints the current branch first. If you've got a better muscle-memory trick for this, I'd like to hear it.