- One More Line
- Posts
- What do you do when you git reset too hard
What do you do when you git reset too hard
How I use git reflog to recover lost commits, plus links to a couple of good reads and a fun little game
Hiya,
I want to share a safety net that I wish someone showed me when I was first learning git - git reflog
.
Imagine we’re exploring a new city. We visit sights, collecting trinkets along the way. As we explore, we keep directions on how to get from one location to another. If we lose a trinket, we can retrace our steps to find it. In this analogy, stealing doesn’t exist.
This describes the git reference log or reflog. To put it another way, reflog is a history of commands that represent the state of your repository - or more accurately, the HEAD - at certain points.
Here's a concrete example; let's say I git reset too hard, and deleted one too many commits.
git reset --hard HEAD~2
Using git reflog
I can see a history of my repository state:
5a660d190 HEAD@{0}: reset: moving to HEAD~2
f345e56d6 HEAD@{1}: commit: Test another commit
e839a7c22 HEAD@{2}: commit: Test commit
A line in the reflog tells us a few things: 5a660d190 HEAD@{0}: reset: moving to HEAD~2
5a660d190 HEAD@{0}
- the SHA and a handy shortcut to reference the SHA.reset
- the git command that was run.moving to HEAD~2
- the result of the command.
To recover the lost commits, we need to know the point before the reset
. That would be HEAD@{1}
. We can then use git reset
with this reference to travel back to that repository state.
git reset --hard HEAD@{1}
And there we go, the lost commits are back on our branch! Thankfully, Git never really loses its history. We can travel back to different points in our repository using reflog
.
Cool stuff around the web
Addy Osmani's latest newsletter issue: Avoiding skill atrophy in the Age of AI highlights the danger of being overly reliant on AI. Maybe we all need to have a No-AI day.
The Boolean Game is a fun little game that teaches you when to use union, subtract, intersect, and difference with shapes. You can use these functions to make icons in Figma!
Ashley Willis shared her fears about being seen I found myself nodding along to the entire post. Willis works through their thoughts about writing publicly. I also love the writing style. It went straight into my writing inspo folder.
Thanks for reading this weeks newsletter. If you have any thoughts or questions, feel free to reach out by replying to this email or send me a DM on Bluesky.
Catch you next week!
Jono