After an evening of happy coding I closed WSL2, a new branch with 3 commits waiting to be pushed the next day. Waking up, I am faced with.
error: object file .git/objects/6a/f509c42dcf23ca0d02292dbca8f1ba042b78db is empty
error: object file .git/objects/6a/f509c42dcf23ca0d02292dbca8f1ba042b78db is empty
error: object file .git/objects/6a/f509c42dcf23ca0d02292dbca8f1ba042b78db is empty
fatal: loose object 6af509c42dcf23ca0d02292dbca8f1ba042b78db (stored in .git/objects/6a/f509c42dcf23ca0d02292dbca8f1ba042b78db) is corrupt
GitLens is also empty, big shame. It seemed like it was interrupted whilst in the process of fixing up my commit, maybe I shut down midway through writing my commit message or maybe WSL2 felt like corrupting my repo, anyways stuck I was.
Don't forget to do a few
cp -R my-project my-project-is-dead-1
and work inside your backups
After some pretty tedious googling and trying solutions on my 10 backups it turned out that the solution was pretty simple.
Our issue here is that our branch is corrupt, for reference mine is called 'core-features-3.' This branch only exists locally and has not been pushed to our remote yet and contains several commits. We can see from the error message above that we are missing an object.
To solve this, my solution was to replace the hash of this damaged loose object with the hash of a previous commit. We cannot use git log
to see our commit history due to corruption, so we can access it through .git/logs/
using nano HEAD
. This should return all of your commits in a list, in my case the last 3 are the commits I want back.
In my case the latest changes are still there in my editor, just with a corrupt git repo. If this is not the case for you this guide will not result in retrival of the latest commit made most likely
780f00510b9d25c5a30183b1f7641f4d0eab6150 94112ae4e16b9dbd26f7ab7926b61d199c1b0fbe felixgren <[email protected]> 1622677346 +0200 checkout: moving from core-features-2 to master
94112ae4e16b9dbd26f7ab7926b61d199c1b0fbe 2d785de1e3b5a81581777073ad8c4eda2f2b3297 felixgren <[email protected]> 1622677350 +0200 pull: Fast-forward
2d785de1e3b5a81581777073ad8c4eda2f2b3297 832c04363cc1d69e6571be3990d843f4aa6e8518 felixgren <[email protected]> 1622386747 +0200 commit: big test
832c04363cc1d69e6571be3990d843f4aa6e8518 2d785de1e3b5a81581777073ad8c4eda2f2b3297 felixgren <[email protected]> 1622386794 +0200 reset: moving to HEAD~
2d785de1e3b5a81581777073ad8c4eda2f2b3297 2d785de1e3b5a81581777073ad8c4eda2f2b3297 felixgren <[email protected]> 1622386997 +0200 checkout: moving from master to core-features-3
2d785de1e3b5a81581777073ad8c4eda2f2b3297 cebec677e60b0050a749bddee5ac06007a5ece52 felixgren <[email protected]> 1622389743 +0200 commit: Refactor function positions
cebec677e60b0050a749bddee5ac06007a5ece52 b54cf4ce7063bc2879ff4f455df3b5904ba63c04 felixgren <[email protected]> 1622453587 +0200 commit: Removed unused code
b54cf4ce7063bc2879ff4f455df3b5904ba63c04 6af509c42dcf23ca0d02292dbca8f1ba042b78db felixgren <[email protected]> 1622554737 +0200 commit: Resolved spawn issue & improved ground detection
Looking at this, we can see that our loose object is the same as the latest commit hash 6af509..
at 'Resolved Spawn issue'. We want to switch this to the hash of the previous commit, in this case b54cf4..
To do this, goto .git/refs/heads
then nano core-features-3
. Here we can see simply one line containing the evil corrupted hash, replace it with the commit hash of the previous hash and save.
Sample
6af509c42dcf23ca0d02292dbca8f1ba042b78db -> b54cf4ce7063bc2879ff4f455df3b5904ba63c04
Refresh vscode and rejoice as git log
returns your commits. The latest one is missing, but the changes for it should all be there in your editor so all that is left is to commit it again.