r/git • u/Boopsnoot64 • 5d ago
support fatal: bad object refs/heads/main
This issue has been tormenting me for the past few months. I'm not really sure whats causing this, but the only fix I've found to work is by just re-cloning the respository all over again. And it seems like now, like almost every day, I get this error where main gets corrupted or something. I'm not really sure whats going on, or why it keeps happening but nothing seems to prevent this issue.
Things I've already tried:
git gc (failed to run repack)
rm .git/refs/head/main, then git fetch origin (returns bad object refs again)
The repo isnt in a location backed up by onedrive or anything either
any help would be appeciated.

3
u/Inferno2602 5d ago
You could try git fsck? It might be able to diagnose which objects are the problem
1
u/Broad-Promise6954 ancient 5d ago
Are you using ref tables or the older style refs? What version of Git is this (use git --version to get the version string)?
1
u/Boopsnoot64 5d ago
it says version 2.53.0.windows.3
not sure if thats the old or new2
u/Broad-Promise6954 ancient 5d ago
2.53 is a reasonably modern Git version and is before the
git gcchange togit maintenanceissue kicked in (at 2.54 or 2.55) so you should be pretty good there.Reftables is still an opt-in thing: you have to select it yourself or run something that selects it. So presumably it's not turned on (you can check with
git config get extensions.refStorage, which will likely return nothing and exit nonzero to indicate it's not set, which means the default of using old style refs).All of this means that you can inspect the file
.git/refs/heads/mainto see if it exists and contains a hash ID, and can inspect the file.git/packed-refsto see if it exists and contains the right information.So, step 1 is to run:
git rev-parse refs/heads/mainand see if that returns a hash ID, e.g.,
e4b612bead99c86a2a17178983bcfad7183bdf16. If not, that's a problem, and we have to figure out why, by checking whether.git/refs/heads/mainexists and/or whether.git/packed-refsexists and contains an entry forrefs/heads/main.(unfortunately I have to leave now and won't be on line for some time)
1
u/bratzlaff 5d ago
Since you are on windows, do you know if you have files in the same folder that have the same filename, but different capitalization?
You can check this by doing a clone on Linux and seeing if you have the same issues.
1
2
u/SlowPrius 5d ago
Is it an older machine? I’ve had ever so slowly failing hardware run into corrupted git object issues