Introduction
You've accidentally lost commits through a destructive git operation like git reset --hard, git rebase, git branch -D, or git push --force. The commits seem gone, but git reflog tracks all HEAD movements and can help you recover the lost work.
Symptoms
After a hard reset:
$ git log --oneline
def456 (HEAD -> main) Previous commit
# Your recent commits are gone!After branch deletion:
$ git branch -D feature-branch
Deleted branch feature-branch (was abc123).
# Branch and its commits are "gone"After force push:
$ git push --force origin main
+ ghi789...abc123 main -> main (forced update)
# Remote commits overwrittenAfter rebase gone wrong:
$ git rebase --abort
# Commits from the rebase seem lostCommon Causes
Git preserves all commits for a period (default 90 days) even after they're no longer reachable from any branch. The reflog records every change to HEAD, creating a history of where you've been. This means "lost" commits still exist in the object database and can be recovered using reflog.
Step-by-Step Fix
Step 1: View the Reflog
See the complete history of HEAD movements:
git reflogOutput shows all recent HEAD positions:
abc123 (HEAD -> main) HEAD@{0}: reset: moving to abc123
def456 HEAD@{1}: reset: moving to def456
ghi789 HEAD@{2}: commit: Add new feature
jkl012 HEAD@{3}: commit: Fix bug in feature
mno345 HEAD@{4}: checkout: moving from feature to main
pqr678 (feature) HEAD@{5}: commit: Work on feature
stu901 HEAD@{6}: branch: Created from mainStep 2: Find the Lost Commit
Search reflog for specific actions:
```bash # Find commits git reflog show --grep="commit"
# Find resets git reflog show --grep="reset"
# Find branch operations git reflog show --grep="branch" ```
View reflog for a specific branch:
git reflog show feature-branchStep 3: Examine the Found Commit
Once you identify a likely commit, examine it:
git show HEAD@{5}Or by commit hash:
git show abc123Check what files changed:
git show --stat HEAD@{5}Step 4: Recover Using Reset
To restore your branch to a previous state:
# Reset to the lost commit
git reset --hard HEAD@{5}Or using the commit hash:
git reset --hard abc123For a softer recovery (keeping current changes):
git reset --soft HEAD@{5}Step 5: Recover a Deleted Branch
To recreate a deleted branch:
```bash # Find where the branch was git reflog show --grep="branch: Created"
# Recreate the branch at that commit git branch recovered-branch HEAD@{5} ```
Or:
git branch recovered-branch abc123Step 6: Cherry-Pick Lost Commits
If you only want specific commits:
```bash # Cherry-pick a single commit git cherry-pick abc123
# Cherry-pick a range git cherry-pick abc123..def456 ```
Step 7: Create a Recovery Branch
Instead of resetting, create a new branch:
git branch recovery-branch HEAD@{5}
git checkout recovery-branchThis preserves your current state while recovering the lost work.
Step 8: Recover After Force Push
If you force-pushed and lost commits on the remote:
```bash # Local reflog still has the commits git reflog
# Reset to the commit before force push git reset --hard HEAD@{1}
# Force push back git push --force origin main ```
Step 9: Find All Dangling Commits
Search for all unreachable commits:
git fsck --lost-foundOutput shows:
dangling commit abc123...
dangling blob def456...
dangling tree ghi789...Examine dangling commits:
git show abc123Or check the lost-found directory:
ls .git/lost-found/commit/Step 10: Extend Reflog Retention
By default, reflog keeps entries for 90 days. To extend:
```bash # Set to 1 year git config --global gc.reflogExpire 365.days
# Keep unreachable objects for 1 year git config --global gc.reflogExpireUnreachable 365.days ```
Prevent garbage collection from cleaning up:
# Disable auto GC temporarily
git config --global gc.auto 0Step 11: Recovery from Remote Reflog
If someone else force-pushed to a shared branch:
```bash # Fetch all refs git fetch origin
# Check remote reflog (if enabled on server) git reflog show origin/main ```
If the commits were pushed and you have a clone, check your local reflog before updating:
# Don't pull yet! Check reflog first
git reflog show origin/mainVerification
Confirm the recovery:
git log --oneline -10Your commits should be back:
abc123 (HEAD -> main) Recovered commit
def456 Previous commitVerify branch exists:
git branchShould show your recovered branch:
main
* recovered-branchCheck file contents:
git show HEAD:path/to/file.txtTo prevent future loss, consider:
```bash # Create a backup branch before risky operations git branch backup-branch
# Or tag important commits git tag important-milestone ```
Related Articles
- [Technical troubleshooting: Fix Git Bisect Skip Marking Bad Commit Incorrectly](bisect-skip-marking-bad-incorrectly-git)
- [Technical troubleshooting: Fix Cherry Pick Range Wrong Order Git Issue in Git](cherry-pick-range-wrong-order-git)
- [Fix Credential Helper Not Storing Wsl2 Git Issue in Git](credential-helper-not-storing-wsl2-git)
- [Fix Git Filter-Branch Rewrite Breaking GPG Commit Signatures](filter-branch-rewrite-breaking-signatures-git)
- [Git Abort Rebase and Recover: Complete Guide](fix-git-abort-rebase)
<script type="application/ld+json"> { "@context": "https://schema.org", "@type": "TechArticle", "headline": "Git Reflog Recovery for Lost Commits", "description": "Learn how to use git reflog to recover lost commits, branches, and work after accidental deletion or destructive operations", "url": "https://www.fixwikihub.com/fix-git-reflog-recovery", "publisher": { "@type": "Organization", "name": "FixWikiHub", "url": "https://www.fixwikihub.com" }, "author": { "@type": "Person", "name": "FixWikiHub Editorial Team" }, "datePublished": "2025-11-18T14:57:31.999Z", "dateModified": "2025-11-18T14:57:31.999Z" } </script>