Skip to content

Recover a deleted Git branch or lost commit with git reflog

Deleted a branch or lost a commit? git reflog keeps recently-referenced commits so you can recover a deleted Git branch and restore a lost commit in seconds.

Deleting a branch with git branch -D feels final, but in most cases the work is still sitting in your repository. Git does not erase the commits when you delete a branch — it only removes the label that pointed at them. Those commits stay in the object database, and Git records where every branch tip and HEAD used to be in a log called the reflog. As long as you are in the same local clone and Git has not run garbage collection yet, you can read that log, find the commit the branch pointed at, and recreate the branch on top of it. This guide walks through the recovery, from the first git reflog to confirming the restored history.

What you’ll accomplish

  • Read the HEAD and branch reflogs to find the commit a deleted branch pointed at.
  • Recreate a deleted branch on its original tip commit with git branch <name> <sha>.
  • Restore a single lost commit onto your current branch with git cherry-pick.
  • Recover a commit that has no reflog entry using git fsck --lost-found.
  • Confirm the recovered history with git log and git show before you continue working.

Prerequisites

  • Git 2.x on your machine (git --version). Every command here works on modern Git; nothing extra to install.
  • Access to the same local repository where the branch or commit existed. The reflog is per-clone and lives in .git/logs/ — it is never pushed to a remote, and a fresh clone starts with an empty reflog.
  • Reasonable timing. Reflog entries expire on a schedule: reachable entries default to 90 days (gc.reflogExpire) and unreachable ones to 30 days (gc.reflogExpireUnreachable). Until then, and until git gc prunes the underlying objects, the commit is recoverable.

How it works

Two mechanisms keep your deleted work alive. The first is the object database. When you commit, Git writes an immutable object identified by its SHA-1 (or SHA-256) hash into .git/objects/. Deleting a branch removes a reference — a 40-character pointer in .git/refs/ — but it does not touch the commit object itself.

The second is the reflog. Every time a reference moves, Git appends a line to its reflog: the old SHA, the new SHA, and a description like commit, checkout, or reset. There is a reflog for HEAD and a separate one for each branch. When you delete a branch you lose that branch’s own reflog, but the HEAD reflog still holds every commit HEAD visited — including the tip of the branch you just deleted.

A commit becomes eligible for permanent removal only when it is both unreferenced and its reflog entries have expired, at which point git gc (or an automatic gc triggered by another command) prunes it. Until that happens, the tip commit is one lookup away.

Timeline showing a deleted branch tip still reachable through the HEAD reflog until git gc prunes it

Step-by-step

1. Open the reflog. Start with the HEAD reflog. It shows, newest first, every position HEAD has held, so a deleted branch’s last commit is almost always near the top.

git reflog
a1b2c3d (HEAD -> main) HEAD@{0}: checkout: moving from feature-login to main
9f8e7d6 HEAD@{1}: commit: Add password reset endpoint
5c4b3a2 HEAD@{2}: commit: Wire up login form validation
0d1e2f3 HEAD@{3}: checkout: moving from main to feature-login

Each line is a candidate. The HEAD@{1} entry above (9f8e7d6) is the last commit made on the now-deleted feature-login branch.

2. Look at the reflog for a specific reference if you know it. git reflog show accepts any ref name. Even after the branch is gone you can scan HEAD explicitly, or check a branch you still have.

git reflog show HEAD
a1b2c3d HEAD@{0}: checkout: moving from feature-login to main
9f8e7d6 HEAD@{1}: commit: Add password reset endpoint

3. Identify the lost SHA. Read the commit messages to find the tip you want. Confirm you have the right one before recreating anything — the SHA on that line is all you need.

git show 9f8e7d6 --stat
commit 9f8e7d6a5b4c3d2e1f0a9b8c7d6e5f4a3b2c1d0e
Author: Dev <dev@example.com>
Date:   Mon Jul 13 09:14:22 2026 -0400

    Add password reset endpoint

 src/auth/reset.js | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)

4. Recreate the deleted branch on that commit. Point a new branch at the SHA. This restores the branch exactly as it was, with its full history intact.

git branch feature-login 9f8e7d6
# no output means success — the branch now exists

5. Check it out in one step instead. If you want to switch to the recovered branch immediately, git checkout -b creates and checks out in a single command.

git checkout -b feature-login 9f8e7d6
Switched to a new branch 'feature-login'

6. Recover a single lost commit with cherry-pick. Sometimes you do not want the whole branch back — you deleted one good commit (a bad reset, an accidental rebase drop) and want it on your current branch. Find its SHA in the reflog, then apply just that commit.

git cherry-pick 5c4b3a2
[main 7a8b9c0] Wire up login form validation
 Date: Mon Jul 13 08:51:03 2026 -0400
 1 file changed, 18 insertions(+)

7. Find a commit that has no reflog entry. If the reflog was cleared, or the commit was created in a way that never moved a ref you still have (for example an interactive rebase that dropped a commit long ago), fall back to git fsck. It scans the object database for commits that nothing references.

git fsck --full --no-reflogs --unreachable --lost-found
Checking object directories: 100% (256/256), done.
Checking objects: 100% (1893/1893), done.
unreachable commit 9f8e7d6a5b4c3d2e1f0a9b8c7d6e5f4a3b2c1d0e
unreachable blob 3b2c1d0e9f8e7d6a5b4c3d2e1f0a9b8c7d6e5f4a

The --lost-found flag writes each dangling commit into .git/lost-found/commit/ so you can inspect it. Take the SHA of the unreachable commit you want and recreate a branch from it exactly as in step 4:

git branch recovered-work 9f8e7d6
# branch created — inspect it before merging anything

Validate it works

Confirm the branch carries the history you expect. git log on the recovered branch should show the commits that build up to the tip.

git log --oneline feature-login -5
9f8e7d6 Add password reset endpoint
5c4b3a2 Wire up login form validation
0d1e2f3 Scaffold login route
b4c5d6e Initial project setup

Then inspect the tip commit’s actual contents to be sure the files are what you remember, not an older or partial version.

git show feature-login
commit 9f8e7d6a5b4c3d2e1f0a9b8c7d6e5f4a3b2c1d0e
...
diff --git a/src/auth/reset.js b/src/auth/reset.js
new file mode 100644

If the log and diff match your memory of the work, the recovery is complete and you can keep working, push, or open a pull request as usual.

Troubleshooting

The reflog entry already expired. If git reflog no longer lists the commit, its entry may have passed gc.reflogExpireUnreachable (30 days by default) and been pruned. Move straight to git fsck --full --no-reflogs --unreachable --lost-found. If fsck also comes up empty, git gc has already deleted the objects and reflog recovery is no longer possible.

You are recovering after a hard reset. A git reset --hard <older-sha> moves your branch back and appears to lose the newer commits, but those commits are still in the HEAD reflog. Run git reflog, find the HEAD@{n} entry from just before the reset, and either git reset --hard <that-sha> to move the branch forward again or git branch salvage <that-sha> to park the work safely first.

You are in the wrong repository. The reflog is per-clone. If you deleted a branch on your laptop and are looking for it in a colleague’s clone or a fresh git clone, it will not be there — that reflog only records what happened in that working copy. You must run recovery in the same local repository where the deletion happened.

Detached HEAD confusion. If a git checkout <sha> left you in detached HEAD and you made commits, then switched branches, those commits have no branch pointing at them. They still show in git reflog. Find the SHA and run git branch <name> <sha> to attach them to a real branch before they age out.

git fsck output is noisy. git fsck reports dangling blobs and trees alongside commits, which is normal and not a problem. Filter to what matters with git fsck --unreachable | grep commit, then use git show <sha> on each candidate commit to find the one you want.

When this is the wrong approach

Reflog recovery resurrects commits that once existed in your local clone. It does not help when:

  • The commit never existed locally. If work was only ever on a teammate’s machine or a remote branch you never fetched, there is nothing in your object database to recover — ask them to push it, or fetch the remote ref.
  • git gc already pruned it. Once objects are gone from .git/objects/, neither reflog nor fsck can bring them back. Restore from a backup, a remote, or a colleague’s clone instead.
  • You want to undo a change, not restore a deletion. If a commit is still in your history and you simply want to reverse its effect, use git revert <sha> to add a new commit that undoes it — that keeps history honest and does not require any reflog archaeology.

For a broader look at keeping branch history clean day to day, see feature-branch Git workflows, and for automating checks on every push, GitHub Actions essentials. The official references are the git reflog documentation and the git fsck documentation.

If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.