tl;dr I did git reset HEAD
without thinking, but was able to recover
my lost work using a helpful git feature that I wanted to highlight. Learn from
my mistakes and how to undo them.
I’ve been working on changes for a local project when my editor started spewing
weird linter errors, so I went digging a little to fix that. This involved
restarting vim several times. My computer had also crashed a couple times in
recent days which left several .swp
files dangling. I had been good about
removing most of them but I missed one .swo
file that was remaining. Long
story short when I recovered from this file it removed several hours worth of
recent changes.
No worries though, I’ll just restore from the last commit right? Except it
wasn’t until after I ran git reset HEAD
that I realized my latest commit
was several hours old. Thankfully I had run git add consistently, so I knew my
changes existed somewhere in my local object store. They just weren’t tied to a
commit or really any tracking tools.
Enter git fsck --lost-found
--lost-found
Write dangling objects into .git/lost-found/commit/ or
.git/lost-found/other/, depending on type. If the object is a blob, the
contents are written into the file, rather than its object name.
My changes existed somewhere as a dangling blob in the object store, so with a list of these blobs I figured I might be able to recover. Turns out it was much easier than I though it’d be.
|
|
Other than just writing out objects to the .git/lost-found
dir, git fsck
also prints the blobs/commits it finds dangling. We can extract these blobs and
grep through them to hopefully find my changes.
|
|
In sum, firstly don’t get into this state
- Commit early and often, you can clean up changes later
- Don’t blindly reload from old swp files
- Don’t blindly run
git reset
If you ignore all that advice though and lose some changes, all hope isn’t lost. Git has some extremely useful recovery tools if you know where to look.