Blog Recover work you thought you lost with git reflog

Recover work you thought you lost with git reflog

2 min read

Recover work you thought you lost with git reflog
TL;DR git reflog is a log of everywhere HEAD has pointed, including commits you reset away or deleted with a branch. Find the commit's hash in the reflog, then check it out, cherry-pick it, or reset back to it. Most lost work in Git is recoverable this way.

Everyone has a moment with Git where their stomach drops. A hard reset that ate uncommitted work, a branch deleted a beat too early, a rebase that went sideways. The good news: Git rarely throws anything away immediately, and reflog is how you get it back.

What reflog remembers

Most Git commands move HEAD, the pointer to where you are. reflog records every one of those moves.

git reflog

You get a list like this:

a1b2c3d HEAD@{0}: reset: moving to HEAD~1
e4f5g6h HEAD@{1}: commit: the work I thought I lost
...

Even when a commit is no longer on any branch, its hash is sitting right there in the reflog.

Recover a lost commit

Find the commit in the reflog, then bring it back.

git reflog                     # locate the hash, e.g. e4f5g6h
git checkout e4f5g6h           # look at it
git reset --hard e4f5g6h       # or move your branch back to it

If you only want that one commit onto your current branch, cherry-pick it instead:

git cherry-pick e4f5g6h

Recover a deleted branch

The branch is gone, but its last commit is not.

git reflog                        # find the branch tip hash
git branch recovered e4f5g6h      # recreate the branch at that commit

Why this works

Git keeps commits around for a while even after nothing references them. Reachable reflog entries stick around roughly 90 days, unreachable ones about 30, before garbage collection may clean them up. So if the mistake was recent, the work is almost always still recoverable.

The habit that saves you

Two things make reflog your safety net. First, commit often, even messy work-in-progress commits, because a commit in the reflog is far easier to recover than uncommitted changes. Second, before any scary command, remember that reflog exists, so you can act calmly instead of panicking. For the wider toolkit, see my Git tricks for untangling a messy branch.

FAQ

What is git reflog?

It is a local log of every position HEAD has taken: commits, resets, checkouts, merges, and rebases. Even when a commit is no longer on any branch, the reflog usually still points to it, which is how you find and recover it.

How long do reflog entries last?

By default, reachable entries are kept about 90 days and unreachable ones about 30, before Git's garbage collection may remove them. In practice, if you lost the work recently, it is almost certainly still there.

Can I recover a deleted branch?

Usually yes. The branch tip commit is still in the reflog. Find its hash with git reflog, then recreate the branch with git branch name hash, or git checkout -b name hash.