How to recover deleted commit in Git

Recovering deleted Git commits is possible because Git retains orphaned commits for a period even after they appear deleted. As the creator of CoreUI with over 25 years of software development experience, I’ve recovered countless accidentally deleted commits. Git’s reflog and fsck commands help locate and restore commits that seem lost after hard resets, rebases, or branch deletions. This approach ensures no work is permanently lost due to Git operations.

Use git reflog and git fsck to find and recover deleted commits by creating new branches or cherry-picking.

Recover after git reset –hard:

# You did hard reset and lost commits
git reset --hard HEAD~5
# Commits appear deleted

# View reflog to find lost commits
git reflog

# Output shows:
# abc1234 HEAD@{0}: reset: moving to HEAD~5
# def5678 HEAD@{1}: commit: Important feature
# ghi9012 HEAD@{2}: commit: Bug fix
# jkl3456 HEAD@{3}: commit: Another change

# Recover by resetting to before the hard reset
git reset --hard HEAD@{1}

# Or create branch at lost commit
git branch recovered-branch def5678

Find specific deleted commit:

# View full reflog with timestamps
git reflog show --all --date=iso

# Search for commit by message
git reflog | grep -i "feature"

# Or search in all refs
git log --all --oneline | grep -i "feature"

# View commit details
git show def5678

# If found, create branch
git branch feature-recovery def5678

Recover with git fsck:

# When reflog doesn't have the commit (expired)
git fsck --lost-found

# Output shows dangling commits:
# dangling commit abc1234567890abcdef1234567890abcdef12345

# Examine dangling commits
git show abc1234567890abcdef1234567890abcdef12345

# Check commit log
git log --oneline --graph abc1234567890abcdef1234567890abcdef12345

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

Recover after rebase:

# After rebase that dropped commits
git rebase -i HEAD~10
# You accidentally dropped important commits

# Find pre-rebase state in reflog
git reflog

# Output:
# abc1234 HEAD@{0}: rebase -i (finish): returning to refs/heads/feature
# def5678 HEAD@{5}: rebase -i (start): checkout HEAD~10
# ghi9012 HEAD@{6}: commit: Lost commit

# Recover the lost commit
git branch lost-commit ghi9012

# Or cherry-pick it to current branch
git cherry-pick ghi9012

Recover multiple deleted commits:

# Find all lost commits from reflog
git reflog | grep "commit:"

# Output shows multiple commits
# abc1234 HEAD@{5}: commit: Feature 1
# def5678 HEAD@{8}: commit: Feature 2
# ghi9012 HEAD@{12}: commit: Feature 3

# Create branches for each
git branch feature-1-recovery abc1234
git branch feature-2-recovery def5678
git branch feature-3-recovery ghi9012

# Or cherry-pick in order
git cherry-pick abc1234 def5678 ghi9012

Recover after amend:

# You amended wrong commit
git commit --amend -m "Wrong amendment"

# Find original commit before amend
git reflog

# Output:
# abc1234 HEAD@{0}: commit (amend): Wrong amendment
# def5678 HEAD@{1}: commit: Original commit

# Recover original
git reset --soft HEAD@{1}

# Or compare changes
git diff HEAD@{1} HEAD

Recover from deleted branch:

# Branch was deleted with commits
git branch -D feature-branch

# Find last commit of deleted branch
git reflog | grep feature-branch

# Output:
# abc1234 HEAD@{10}: checkout: moving from feature-branch to main
# def5678 HEAD@{11}: commit: Last commit on feature-branch

# Recreate branch
git branch feature-branch def5678

Search through all dangling commits:

# Get all dangling commits
git fsck --no-reflog | grep commit

# Create script to check each one
for commit in $(git fsck --no-reflog | grep commit | awk '{print $3}'); do
  echo "=== Commit $commit ==="
  git log -1 --oneline $commit
  echo ""
done

# When you find the right one
git branch recovered-work <commit-hash>

Recover specific file from deleted commit:

# Find commit that had the file
git reflog -- path/to/file.js

# Output shows when file was modified
# abc1234 HEAD@{5}: commit: Updated file.js

# Restore file from that commit
git checkout abc1234 -- path/to/file.js

# Or view file content
git show abc1234:path/to/file.js

Check what will be recovered:

# Before recovering, check commit content
git log -1 abc1234

# View files changed
git show --stat abc1234

# View full diff
git show abc1234

# View commit ancestry
git log --oneline --graph abc1234

Time limits for recovery:

# Reflog entries expire after 90 days (default)
git config --get gc.reflogExpire
# Output: 90.days.ago

# Unreachable objects kept for 30 days
git config --get gc.pruneExpire
# Output: 2.weeks.ago

# View when reflog will expire
git reflog --date=iso

# Prevent automatic cleanup (temporarily)
git config gc.auto 0

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

Recover merge commit:

# Merge commit was lost
git reflog | grep -i merge

# Output:
# abc1234 HEAD@{5}: merge feature: Merge made by 'ort' strategy

# Recover merge
git branch recovered-merge abc1234

# Or recreate merge
git merge --no-ff <branch-that-was-merged>

Export dangling commits:

# Export all dangling commits for review
git fsck --lost-found > lost-commits.txt

# Create patches from dangling commits
for commit in $(git fsck --no-reflog | grep commit | awk '{print $3}'); do
  git format-patch -1 $commit --stdout > "patches/$commit.patch"
done

# Apply patch later
git apply patches/<commit-hash>.patch

Prevent commit loss:

# Create backup branch before risky operations
git branch backup-$(date +%Y%m%d)

# Tag important commits
git tag important-work

# Push branches to remote regularly
git push origin --all

# Enable reflog on remote (if supported)
git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'

Best Practice Note

Git reflog stores commit history for 90 days by default, making recovery possible months after deletion. Use git fsck --lost-found when reflog has expired. Always check commit content with git show before recovering to ensure it’s the right one. Create a new branch for recovered commits rather than resetting directly. The reflog is local—each developer has their own, so you can’t recover other developers’ deleted commits. Tag or back up important commits before risky operations. This is how we recover deleted commits in CoreUI development—using reflog as our safety net, thoroughly checking commit content before recovery, and ensuring no valuable work is permanently lost.


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