Git Reflog Tutorial: Recover Lost Commits and Abandoned Branches
Master the git reflog tutorial to recover lost git commits and undo git reset. Learn the exact steps to rescue a deleted branch before it's gone forever.
We’ve all been there: you’re deep into a feature, you run a git reset --hard to clean up a mess, and suddenly realize you just nuked three hours of solid coding. Panic sets in, but before you start rewriting your logic, remember that Git is designed to be virtually bulletproof. The reflog is your safety net, and it's saved my neck more times than I care to admit.
Understanding the Git Reflog Tutorial
Most developers think git log is the source of truth, but it only shows the reachable history of your current branch. When you delete a branch or perform a destructive action like a reset, those commits become "orphaned"—they still exist in your local repository, but they aren't linked to any branch.
The git reflog (reference log) acts as a local-only history of where your HEAD pointer has been. It tracks every time you’ve checked out a branch, committed, or performed a reset. It’s essentially an undo button for your entire local Git workflow.
Why the Reflog is Your Last Line of Defense
If you’ve accidentally run a git reset --hard or merged into the wrong branch, the standard git log will look like the work is gone. If you're trying to Git Rebase vs. Merge: A Guide to Clean Commit History and things go sideways, the reflog is often the only way to get back to a sane state.
How to Recover Lost Git Commit Objects
To recover a lost git commit, you first need to identify the exact state you want to return to. Open your terminal in your project directory and run:
Bashgit reflog
You’ll see output that looks something like this:
TEXTa1b2c3d HEAD@{0}: reset: moving to HEAD~1 e4f5g6h HEAD@{1}: commit: Finalized the payment gateway logic i7j8k9l HEAD@{2}: checkout: moving from feature-branch to main
In this case, e4f5g6h is the commit I "lost" when I ran the reset. To get it back, you have two primary options.
Option 1: Cherry-pick the commit
If you just want the changes back on your current branch, use git cherry-pick:
Bashgit cherry-pick e4f5g6h
Option 2: Hard reset to the previous state
If you want to move your branch pointer back to that specific moment in time:
Bashgit reset --hard e4f5g6h
Warning: Be careful with reset --hard. It will overwrite any uncommitted changes in your working directory. Always git stash if you have active work you don't want to lose.
How to Undo Git Reset and Rescue Deleted Branches
Sometimes you don't just lose a commit; you delete an entire branch. If you accidentally ran git branch -d and lost your progress, the reflog still tracks the branch's last known position.
To rescue a deleted branch, find the commit hash where the branch was last active in the reflog, then recreate the branch from that point:
Bash# Find the hash from the reflog output git checkout -b recovered-branch-name <commit-hash>
This effectively "undo git reset" or "undo branch deletion" by manually re-pointing a branch reference to the orphaned commit. It’s a clean, surgical way to fix mistakes without needing to reach for remote backups or complex recovery tools.
| Action | Command | Purpose |
|---|---|---|
| View History | git reflog | Shows all movements of HEAD |
| Recover Commit | git cherry-pick <hash> | Pulls a lost commit into your current branch |
| Restore Branch | git checkout -b <name> <hash> | Creates a new branch from a lost commit |
| Force Undo | git reset --hard <hash> | Moves current branch to a previous state |
Common Pitfalls and Limitations
The reflog isn't magic. There are a few things you need to keep in mind:
- It’s Local Only: The reflog is stored in
.git/logs/. If you delete your local repository or move to a different machine, the reflog is gone. It doesn't sync with remote repositories like GitHub or GitLab. - Expiration: Git periodically prunes old entries from the reflog (usually after 30 to 90 days). Don't wait weeks to fix a major screw-up.
- Garbage Collection: If you run
git gc(garbage collection), Git might permanently delete orphaned objects that aren't referenced by any branch or tag. If you've run a manual garbage collection, the reflog might show the hash, but the actual data could be gone.
I once spent about four hours trying to figure out why my HEAD was detached after a botched merge. I kept looking at the remote, thinking the server had the answer. Once I realized the local reflog had the truth, it took me less than two minutes to restore the branch.
Next time you feel that sinking feeling of "I just deleted my work," take a breath, close your IDE, and open your terminal. The reflog is almost certainly holding the key to your recovery. Just remember to verify the commit hash before you start resetting your branch—it's easy to get the order of operations wrong when you're already stressed.


