Recover Lost Work (reflog)

The symptom

You ran git reset --hard and went one commit too far. Or you deleted a branch with git branch -D and realised a second later it held real work. Or you were browsing an old commit in "detached HEAD" state (Git's way of saying you're not on any branch), made a commit there, switched away — and now it's nowhere. The panic is the same every time: my work is gone.

What you might see

Terminal
$ HEAD is now at 3f2e1d0 Old homepage layout
$ Deleted branch login-page (was a1b2c3d).
In VS Code
  1. No error toast appears — VS Code thinks everything is fine
  2. Your recent edits simply vanish from the editor, and the commits disappear from the Source Control graph

This is Git's message, not commands to run.

First, breathe — Git rarely loses anything. Almost certainly, your work is still there.

What it means

Here's the calm truth: Git doesn't delete commits right away. Commands like reset --hard and branch -D don't erase snapshots — they only move or remove the labels pointing at them. The commits themselves sit quietly in Git's storage, unlisted but intact, for weeks.

Better still, Git keeps a diary. It's called the reflog (short for "reference log"): a private, local list of every place HEAD — your current position — has ever been. Every commit, switch, merge, pull and reset adds a line. Entries stick around for roughly 90 days, and the reflog never leaves your machine — it isn't pushed to GitHub, and nobody else can see it.

Mental model: commits are photos; branches are sticky notes on them. reset --hard and branch -D only peel off sticky notes — the photos stay in the shoebox. The reflog is Git's list of every photo you've recently held.

Find the lost commit

Run git reflog. The newest entries are at the top, and each line shows a short hash (the commit's ID), where HEAD was, and what moved it — things like commit, checkout or reset. Scan down for the entry from just before the mistake; its message should read like your lost work.

Read the diary

Terminal
$ git reflog
$ # find the entry from just before the mistake, e.g. a1b2c3d
In VS Code
  1. This is a terminal-only rescue — VS Code has no reflog view. Open the integrated terminal (Terminal → New Terminal) and run it

Press q to leave the list if it opens in a pager. reflog entries stick around for weeks, so recovery is usually possible.

Restore it

Once you have the hash, you have two options. The direct one: move your branch back to that commit with reset --hard. The gentler one: leave your current branch alone and park the lost commit on a brand-new branch, so you can look it over first.

Bring the commit back

Terminal
$ git reset --hard a1b2c3d     # put the branch back there
$ # or park it on a new branch instead:
$ git branch rescued-work a1b2c3d
In VS Code
  1. Also terminal-only — run it in the integrated terminal
  2. Afterwards, your files and the Source Control panel snap back to that point

Nervous? Use the git branch option — it changes nothing about where you are now, it just puts a fresh label on the lost commit.

What reflog can't save

One honest limit: the reflog only records commits. If you discarded edits with git restore, or deleted untracked files with git clean, and that work was never committed (or stashed), Git never took a photo of it — there is nothing in the shoebox to find. The same goes for a file you deleted in your editor before ever staging it. For those, your only hopes are your editor's local history or a backup.

Tip: commit early, commit often. A rough message on a work-in-progress commit costs nothing — you can tidy it later with amend or reset — but everything committed is recoverable for weeks. Uncommitted work has no safety net.

Up next: the everyday wall everyone hits — GitHub refuses your push. What "rejected" really means and the two safe ways past it: Push Rejected.

FAQ

Frequently Asked Questions

I ran git reset --hard and lost work — can I get it back?
Usually yes. git reflog lists every recent position of HEAD, including "lost" commits. Find the hash from just before the mistake and run git reset --hard <hash> to restore it.
How long do reflog entries last?
By default Git keeps reflog entries for about 90 days (30 days for commits no longer reachable from any branch) before garbage collection may prune them. In practice you have weeks to notice a mistake and recover.
Can reflog recover a deleted branch?
Yes. Deleting a branch only removes the label, not the commits it pointed to. Run git reflog, find the hash the branch tip was at, then git branch rescued-work <hash> to give it a new name.
Can reflog bring back changes I never committed?
No. The reflog only records commits, so edits discarded with git restore or files deleted with git clean before any commit are truly gone. Commit early and often — everything committed is recoverable; see Undo Commits for the commit-level toolkit.