Ever experienced a moment when your code editor or terminal crashes and you lose the changes you’ve just made? Or a scenario where you are about to reset a Git HEAD, amend or rebase a commit, and suddenly your Git history is out of control because your editor or terminal crashed?
In this article I will walk you through the power of git reflog
.
Hinting at the power of Git
Git is a tool that helps developers maintain version control and track every change in a codebase. It enables collaboration, simplifies code review, and provides a safety net when things go wrong. It is essential for every developer to know how to use Git commands effectively.
What is git reflog
?
While git log
shows the history of all finalised commits in your branch, git reflog
records every change to HEAD
and branch tips, even if those commits are no longer reachable by any branch. It shows you exactly when you started a rebase or reset, which commits were applied or skipped, and when the rebase or other operations finished.
Why is git reflog
helpful?
Take a look at the screenshot below of a git rebase
. You can see the start of the rebase, each step where a commit was picked, and the finish when the operation completed, all labeled by entries like HEAD@{15}
.
git reflog
becomes extremely useful because it lets you checkout any of those HEAD
states and recover your code exactly as it was before a crash or unintended change. You can jump back to where you need to be, even if the commit is no longer part of your visible history.
When do you need git reflog
?
Here is a real example from my own experience:
- I made a commit, then realized I needed to adjust part of it.
- I ran
git reset --soft HEAD^
to go back and make those changes. - In the middle of editing, my code editor froze. When I restarted, I discovered a rebase had started on another branch without my input.
- I aborted the rebase, but lost the uncommitted changes I was working on.
At this point I ran git reflog
and saw the exact entries for the rebase start and finish. I found the HEAD
state just before the rebase began (even though it had the same commit hash) and reset back to it. Thanks to git reflog
, I recovered all my changes without losing a single line of code.
That is why git reflog
is so powerful. It shows you every HEAD
movement for at least 30 days by default. You can see where a rebase started, where it ended, and every reset or checkout in between.
Scenarios when you may need git reflog
- You ran a
git reset
orgit rebase
and realise you reset too far. - Your editor or terminal crashed during a Git operation.
- You accidentally amended or squashed a commit you still needed.
- You switched branches and then realised you needed the unmerged work.
- You want to recover lost references after a forced push or merge conflict.
Other useful Git commands
There are many Git commands that complement git reflog
and help you manage your history safely:
-
git stash
to save uncommitted changes before switching tasks. -
git reset --soft
to moveHEAD
without losing your working directory changes. -
git cherry-pick
to apply a single commit from another branch. -
git revert
to create a new commit that undoes a previous one.
Learning these commands gives you fine-grained control over your history and workflow.
Leverage Git like a superpower
When you master Git, you can recover from almost any mistake. You gain confidence to try daring refactors, experiment without fear, and fix critical bugs under pressure.
Always push to a remote branch
While git reflog
can save you locally, it is still best practice to push your work to a remote repository frequently. If you are mid-feature, add a WIP
prefix (Work In Progress) to your commit messages so teammates know it is not final. When the feature is complete, squash or clean up your commits before merging.
I once had my PC die without warning on a Monday morning and I had few days left to deadline. Fortunately I had pushed every change to a remote branch. I cloned the repo on another machine and continued coding immediately. I lost only the hardware, not my work.
Conclusion
git reflog
is your safety net when Git operations go sideways. It records every HEAD
movement so you can recover lost commits, undone changes, and interrupted rebases. Combine it with git stash
, git reset
, and regular pushes to a remote repo, and you will never have to fear losing your code again.
Thank you for reading. I hope this makes you more confident in handling Git. Share your own stories or questions in the comments, I would love to hear how git reflog
or a similar git command has saved your day.
You can always reach out if you have any gigs, offers, or questions!
Stanley Azi
Full‑Stack Engineer | Frontend Specialist
Top comments (1)
I think this is an amazing, yet very simple recovery tool that every engineer needs in their tool set.
Thank you for showing us the amazing
git reflog