Context: Why Git Rarely Ever Truly Loses Your Code
You just finished grinding out 300 lines of payment-processing code at 2 AM. Drowsy and unfocused, instead of typing git reset --soft, you mistakenly run git reset --hard HEAD~1. You check git log, and your commit is completely gone. Don’t panic.
Git is built around an exceptionally safe snapshot model. Under the hood, Git almost never immediately purges any commit you’ve created locally. The git log command only traverses commits directly reachable from your current branch.
When you reset or rebase over commits, the branch pointer moves elsewhere. The older commits are detached from the history tree, becoming “dangling commits” (or orphan commits). All their snapshot data remains intact inside the .git/objects directory for at least 30 to 90 days before garbage collection sweeps them away.
To track down these lost commits, you use Git Reflog (Reference Logs). This mechanism logs every single movement of the HEAD pointer: from commits, checkouts, and cherry-picks to rebases and resets. As long as you committed your code locally at least once, reflog lets you retrieve the commit hash in mere seconds.
Enabling and Configuring Reflog in Your Workflow
Git reflog is enabled by default across all standard (non-bare) repositories on Linux, macOS, and Windows.
If you’re operating on a server or working with a bare repository, check the core.logAllRefUpdates configuration setting:
# Check current status
git config core.logAllRefUpdates
# Enable logging if the output returns false or empty
git config core.logAllRefUpdates true
When disaster strikes, reading logs with clear relative timestamps makes troubleshooting much easier. You should set up an alias in your ~/.gitconfig file:
git config --global alias.rl "reflog --date=relative"
When you run git rl, your terminal displays human-friendly relative timestamps like “15 minutes ago” instead of cryptic raw indices.
3 Real-World Data Recovery Scenarios Using Reflog
The core syntax of reflog references is HEAD@{index}, where HEAD@{0} represents your current position, HEAD@{1} is the state immediately prior, and larger numbers reach further back in time.
Scenario 1: Recovering a Commit After an Accidental git reset –hard
Suppose you are on the feature/stripe-billing branch and just committed a webhook feature, but accidentally ran:
git reset --hard HEAD~1
All modified files in your working directory have vanished. Stay calm and follow these three steps:
- Check the HEAD movement history:
git reflogYour screen will show output similar to this:
a1b2c3d HEAD@{0}: reset: moving to HEAD~1 e4f5g6h HEAD@{1}: commit: feat: handle stripe webhook invoice.paid 7i8j9k0 HEAD@{2}: checkout: moving from main to feature/stripe-billing - Identify the target: commit
e4f5g6hatHEAD@{1}is precisely where your code lived right before the reset. - Restore your code. You have two options:
# Option 1 (Recommended): Create a new branch from the old commit for safe inspection git branch recovery-webhook e4f5g6h # Option 2: Move HEAD on the current branch directly back to that commit git reset --hard HEAD@{1}
Scenario 2: Recovering After a Failed Interactive Rebase
While squashing 4 commits into 1 using git rebase -i HEAD~4, you accidentally picked drop or botched a conflict resolution, losing code in the process. The botched rebase leaves your branch history in disarray.
Simply open the reflog to pinpoint the state immediately prior to starting the rebase:
git reflog
# Find the line: rebase (start): checkout main (or the commit before running rebase)
# Assume the corresponding hash is 9x8y7z6 at HEAD@{6}
git reset --hard HEAD@{6}
This command completely aborts the flawed rebase session and restores your branch to its original state.
Scenario 3: Recovering a Branch Deleted with git branch -D
While cleaning up your repository, you accidentally run git branch -D feature/auth-jwt before merging it into main. Git deleted the branch pointer, but the tip commit of that branch is still recorded in the reflog.
Filter the checkout history to locate the last commit hash:
git reflog | grep "checkout: moving from feature/auth-jwt"
The result points directly to the last commit hash before you switched branches. Simply recreate the branch from that hash:
git checkout -b feature/auth-jwt 3c4d5e6
Configuring Reflog Cleanup Cycles (Retention Period)
Reflog does not store entries indefinitely. By default, Git keeps reflog entries for reachable commits for 90 days and unreachable (dangling) commits for 30 days. To enhance safety on long-term projects, you can extend these retention windows:
# Keep reflog for reachable commits for 180 days
git config --global gc.reflogExpire "180 days"
# Keep unreachable commits for 60 days
git config --global gc.reflogExpireUnreachable "60 days"
Battle-Tested Tips for Code Recovery
Don’t rush to run git reset --hard the second you spot a hash in reflog. Inspect the diff first to make sure you’re targeting the right commit:
# Compare current state with the suspected point at HEAD@{2}
git diff HEAD HEAD@{2}
# Or inspect the commit details directly
git show HEAD@{2}
You can also view the full timeline of HEAD movements as a concise list:
git log -g --oneline --decorate
Key Takeaway: Git Reflog exists solely on your local machine and is never pushed to GitHub or GitLab via git push. If you accidentally lose code, stay on the local machine where the actions occurred to resolve it. Under no circumstances should you delete your project directory or re-clone the remote repository, as doing so permanently wipes out this invaluable reflog history.

