Recover a Deleted Git Branch with reflog
Use git reflog to find and restore a branch you accidentally deleted with git branch -D or a botched rebase.
git reflog is git's local-only undo history. Every checkout, commit, reset, rebase, and branch deletion leaves a trace there. When you accidentally git branch -D feature/important and panic, reflog is the rescue. The commit isn't gone — git just orphaned it, and reflog tells you the SHA.
Tested on git 2.43+.
When to Use This
- You ran
git branch -Don the wrong branch - A
git rebasewent sideways and you want to get back to the pre-rebase state - A
git reset --hardwiped uncommitted commits - You force-pushed and need to find the old commit
Don't use this when the changes were never committed (reflog only tracks committed state) or when you're on a different machine than where the work happened (reflog is per-clone).
Code
# Show every move HEAD has made (most recent first)
git reflog
# Filtered to just one branch's history
git reflog show feature/login
# With full commit messages
git reflog --pretty=format:'%h %gd %gs'Sample output:
a1b2c3d HEAD@{0}: checkout: moving from feature/login to main
e4f5g6h HEAD@{1}: commit: feat: add login form validation
i7j8k9l HEAD@{2}: commit: feat: scaffold login form
m0n1o2p HEAD@{3}: checkout: moving from main to feature/login
HEAD@{N} is shorthand for "where HEAD was N moves ago". You can use these references in any git command.
Usage
The full recovery flow for an accidentally deleted branch:
# 1) You realize you deleted feature/login by mistake
$ git branch -D feature/login
Deleted branch feature/login (was e4f5g6h).
# 2) Look at reflog to find the last commit on that branch
$ git reflog
e4f5g6h HEAD@{1}: commit: feat: add login form validation
i7j8k9l HEAD@{2}: commit: feat: scaffold login form
# 3) Recreate the branch from the commit SHA
$ git branch feature/login e4f5g6h
# 4) Verify
$ git log feature/login --oneline
e4f5g6h feat: add login form validation
i7j8k9l feat: scaffold login formYou're back. The branch is restored with all its commits intact.
A second scenario: undo a botched interactive rebase.
# After a rebase you regret
$ git reflog
abcdef0 HEAD@{0}: rebase (finish): returning to refs/heads/feature/login
abcdef1 HEAD@{1}: rebase: feat: zod validation
abcdef2 HEAD@{2}: rebase (start): checkout HEAD~5
e4f5g6h HEAD@{3}: commit: feat: add login form validation ← pre-rebase
# Reset to the pre-rebase state
$ git reset --hard HEAD@{3}Pitfalls
- Reflog is local only. It lives in
.git/logs/. If you cloned fresh after the disaster, the reflog of the original clone is gone with it. - Reflog entries expire. Default expiry is 90 days for reachable commits and 30 days for unreachable ones. After that,
git gccan prune them. HEAD@{1}versusHEAD~1is a common confusion.HEAD~1is "the parent of HEAD" (a commit before in the graph).HEAD@{1}is "HEAD's previous position" (a commit before in time on this machine).- Reflog only tracks HEAD by default. For per-branch reflog, use
git reflog show <branch>. Each branch has its own log. - Don't rely on reflog as a backup. It's a local safety net for accidents, not a substitute for pushing to a remote.
Related Snippets & Reading
- Undo the Last Git Commit — for recent commits before they need rescuing
- Interactive Rebase to Squash Commits — the operation reflog often saves you from
- git reflog official docs — full reference
Frequently Asked Questions
What is git reflog and how is it different from git log?
git log shows commits reachable from the current branch. git reflog shows every move HEAD has made on your local machine — checkouts, commits, resets, rebases, even deleted branches. It's a local-only safety net that nothing in git removes for ~90 days by default.
Can git reflog recover commits from a force-pushed branch?
It can recover commits that existed on YOUR local machine. If you force-pushed and your local branch still has the old commits, reflog finds them. If you only ever pulled the new state, the old commits are gone unless someone else's reflog has them.