Recover lost Git commits or a deleted branch with reflog
Short answer: If you've lost commits after a bad reset, a botched rebase or deleting a branch, they're almost certainly still in your local repository. Run git reflog to see where your branch and HEAD pointed recently, find the commit from before the mistake, and create a branch there with git branch rescue <commit>. Reflog entries are kept for weeks by default, so act soon, and don't run cleanup commands until you've recovered what you need.
Why "lost" commits usually aren't
Git rarely deletes anything straight away. When you reset a branch, rebase, amend a commit or delete a branch, the old commits stop being reachable from any branch name, so they disappear from git log. But the objects remain in the repository until garbage collection eventually removes them.
The reflog is what lets you find them again. Git's documentation describes reference logs as records of when the tips of branches and other references were updated in the local repository. Every time HEAD moves, through a commit, checkout, reset, merge or rebase, Git writes an entry.
Typical situations where this saves a developer's afternoon:
git reset --hardto the wrong commit, wiping a day's work.- A rebase that went wrong and was "fixed" by more resets.
- Deleting a feature branch that hadn't been merged.
- Amending a commit and losing the previous version.
- A messy pull that replaced local work.
Step 1: look at the reflog
Run git reflog. You'll see a list like this, newest first:
a1b2c3d HEAD@{0}: reset: moving to HEAD~3e4f5a6b HEAD@{1}: commit: Add invoice PDF export9c8d7e6 HEAD@{2}: commit: Fix VAT rounding on totals
Each line shows a commit hash, a position such as HEAD@{2}, and what happened. Git's docs explain the notation: HEAD@{2} means where HEAD was two moves ago, and you can also use time-based forms like main@{one.week.ago}, meaning where main pointed a week ago in this repository.
Find the last entry before the mistake. In the example, HEAD@{1} is the commit just before the accidental reset.
Step 2: inspect before you move anything
Check you've got the right commit:
git show e4f5a6bshows that commit's changes.git log e4f5a6bshows its history, so you can confirm the earlier commits are there too.
If you're unsure which entry is right, look at a few. They're cheap to inspect.
Step 3: recover safely
The safest move is to create a new branch pointing at the recovered commit:
git branch rescue e4f5a6b
Now nothing can garbage-collect it, and you can compare it with your current branch at leisure. Once you're happy, you can merge it, cherry-pick from it, or reset your branch to it.
If you're certain and want your current branch back where it was, git reset --hard e4f5a6b moves it there directly. Make sure you have no uncommitted changes you want to keep first, because a hard reset discards them.
Recovering a deleted branch
Deleting a branch removes its own reflog, but HEAD's reflog still records the commits you made or checked out on it. Search the output of git reflog for the branch's last commit message or for a "checkout: moving from feature-x" line, then create the branch again at that commit. When Git deletes a branch with git branch -D, it also prints the commit hash it pointed to, so check your terminal history first.
How long you have
Reflog entries don't last forever. The git gc documentation lists the defaults: entries older than 90 days are removed (gc.reflogExpire), and entries that are no longer reachable from the current branch tip are removed after 30 days (gc.reflogExpireUnreachable). Lost commits fall in the second group, so think in terms of weeks, not months. Garbage collection can run automatically during normal Git commands.
Until you've recovered your work, avoid commands that prune: git gc --prune=now, git reflog expire and similar cleanup.
What the reflog can't recover
- Uncommitted changes. If work was never committed or stashed, the reflog has no record of it. Your editor's local history may still have it.
- Work from another computer. The reflog is local. A colleague's lost commits are in their reflog, not yours.
- A fresh clone. Cloning starts a new reflog. If you deleted your old copy of the repository, its reflog went with it.
Shared branches on your Git host may keep their own activity history, which can also help, depending on the host.
Habits that prevent the panic
- Commit small and often, even on a work-in-progress branch.
- Push feature branches to the remote regularly, so a copy exists elsewhere.
- Before a risky rebase or reset, create a quick backup branch:
git branch backup-before-rebase. - Prefer
git reset --keeporgit switchover--hardwhen you're not sure.
When the reflog looks confusing
A long reflog can be hard to read after a chaotic hour. A few tricks help:
git reflog --date=isoadds timestamps, so you can find the moment just before things went wrong.git log -g --onelineshows the same list in a log-style view.- Searching for a commit message you remember is often faster than reading line by line.
A worked example
Picture a hypothetical Ottawa web agency developer tidying a feature branch before review. An interactive rebase went wrong and, trying to start over, she ran git reset --hard origin/feature and lost two days of local commits that hadn't been pushed. A colleague suggested git reflog. The entry just before the reset pointed to her last good commit. She created rescue-feature there, checked the history with git log, then reset her branch to it and pushed straight away.
FAQ
Does git reflog work on remote repositories?
No. It records changes in your local repository only.
Can I recover a dropped stash?
Often, yes. Dropped stash commits can sometimes be found among unreachable objects using git fsck, though that's more involved than reading the reflog.
Is it safe to look at the reflog?
Completely. Viewing it changes nothing. Only expire and delete subcommands remove entries.
Will running git gc delete my lost commits?
It can remove unreachable commits older than the expiry settings. Create a branch on anything you want to keep first.
If your team keeps losing work to Git mishaps, tell us how you work with branches and we'll suggest a safer workflow.