How to recover deleted branch in Git

Recovering deleted Git branches is possible because Git retains commits for a period even after branch deletion. As the creator of CoreUI with over 25 years of software development experience, I’ve recovered many accidentally deleted branches. Git’s reflog tracks all branch operations including deletions, allowing you to find the commit hash and recreate the branch. This approach helps you restore deleted work without losing commits or branch history.

Use git reflog to find the deleted branch’s commit hash and recreate the branch pointing to that commit.

Recover recently deleted local branch:

# You accidentally deleted a branch
git branch -d feature-branch
# Or force deleted: git branch -D feature-branch

# View reflog to find the branch tip
git reflog

# Output shows:
# abc1234 HEAD@{0}: checkout: moving from feature-branch to main
# def5678 HEAD@{1}: commit: Last commit on feature-branch
# ghi9012 HEAD@{2}: commit: Previous commit

# Recreate branch at the last commit
git branch feature-branch def5678

# Or use HEAD reference
git branch feature-branch HEAD@{1}

# Switch to recovered branch
git checkout feature-branch

Recover branch with specific reflog:

# View reflog for all branches
git reflog show --all

# Find deleted branch entries
git reflog | grep feature-branch

# Output shows:
# abc1234 feature-branch@{0}: commit: Final commit
# def5678 feature-branch@{1}: commit: Previous work

# Recreate using commit hash
git branch feature-branch abc1234

Recover deleted remote branch:

# Someone deleted a remote branch
git fetch --prune

# Check reflog for remote references
git reflog show origin/feature-branch

# If reflog still exists, find last commit
# abc1234 origin/feature-branch@{0}: update by push

# Recreate local branch
git branch feature-branch abc1234

# Push to remote
git push origin feature-branch

Find commit when you don’t know branch name:

# You deleted a branch but forgot its name
git reflog | grep -i "commit:"

# Look for recent commits that might be from deleted branch
# abc1234 HEAD@{5}: commit: Implemented feature X

# Check what that commit contains
git show abc1234

# If it's the right commit, create branch
git branch recovered-branch abc1234

Recover using fsck (when reflog expired):

# Reflog expired but commits still exist
git fsck --lost-found

# Shows dangling commits
# dangling commit abc1234def5678...

# Examine dangling commits
git show abc1234def5678

# If it's your deleted branch, create branch
git branch recovered-branch abc1234def5678

Recover multiple deleted branches:

# View all recent branch operations
git reflog | grep "checkout:"

# Find branch names and commit hashes
# abc1234 HEAD@{3}: checkout: moving from feature-1 to main
# def5678 HEAD@{5}: checkout: moving from feature-2 to main

# Recreate branches
git branch feature-1 abc1234
git branch feature-2 def5678

Recover branch deleted with force:

# Branch was force deleted with -D
git branch -D unmerged-feature

# Same recovery process
git reflog

# Find last commit
# abc1234 HEAD@{1}: commit: Unmerged work

# Recreate branch
git branch unmerged-feature abc1234

Check branch contents before recovering:

# Find the commit hash
git reflog | grep branch-name

# View commits on that branch
git log abc1234 --oneline

# View files changed
git show abc1234 --stat

# View full diff
git diff abc1234~5..abc1234

# If correct, recover
git branch branch-name abc1234

Recover branch from other developer:

# Someone deleted a branch you need
# Ask them for the commit hash
# Or if they pushed it before

# Check if commit exists in remote
git ls-remote origin

# Fetch all references
git fetch origin

# Find commit in fetched data
git log --all --oneline | grep "commit message"

# Create local branch
git branch recovered-branch abc1234

Prevent accidental deletion:

# Use -d instead of -D (safer deletion)
git branch -d feature-branch
# Error if branch not merged

# Force delete only when sure
git branch -D feature-branch

# Create backup before deletion
git branch backup-feature feature-branch
git branch -D feature-branch
# Keep backup until verified

Time limits for recovery:

# Reflog keeps entries for 90 days by default
git config --get gc.reflogExpire
# Output: 90.days.ago

# Unreachable commits kept for 30 days
git config --get gc.reflogExpireUnreachable
# Output: 30.days.ago

# Run garbage collection manually (careful!)
git gc --prune=now

Verify recovered branch:

# After recovery, verify commits
git log recovered-branch --oneline

# Check if all expected commits present
git log main..recovered-branch

# Compare with expected state
git diff main recovered-branch

# If correct, rename if needed
git branch -m recovered-branch original-name

Recover branch with complex history:

# Find merge commits involving the deleted branch
git reflog | grep -i merge

# abc1234 HEAD@{10}: merge feature-branch: Merge made

# Create branch before the merge
git reflog show abc1234
# Find the feature branch tip before merge

# Recreate branch
git branch feature-branch def5678

Script to list recently deleted branches:

# Show all deleted branches from reflog
git reflog | grep -E "checkout: moving from" | \
  awk '{print $6}' | sort | uniq

# Or more detailed
git reflog | grep -E "branch|checkout" | head -20

Best Practice Note

Git reflog stores branch operations for 90 days by default, making recovery possible for months after deletion. Use git branch -d instead of -D to prevent deleting unmerged branches. Always verify branch contents with git show or git log before recovering. The reflog is local to your repository—each developer has their own. Run git fsck --lost-found when reflog has expired. Create backups before force-deleting important branches. This is how we recover deleted branches in CoreUI development—using reflog to find commit hashes, verifying branch contents, and safely recreating branches without losing valuable work.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author