I used git-replace and git-mktree to fix this in the past. You essentially keep the broken tree object, but override all links and make them point to a new object.
First we grab the bad tree:git ls-tree bad_tree_hash > tmpfile.txt
This writes out your bad tree. For example:
040000·tree·3cdcc756ee0ed636c44828927126911d0ab28a18 → xNotAlphabetic
040000·tree·4ad0d8ef014b8cc09c95694399254eff43217bfb → EXT
040000·tree·d65085e4a05ea9ac8b79e37b87202dd64d402c2e → duplicateFolder
040000·tree·d65085e4a05ea9ac8b79e37b87202dd64d402c2e → duplicateFolder
040000·tree·fd0661d698ace91135a8473b26707892b7c89c32 → ToolTester
040000·tree·d65085e4a05ea9ac8b79e37b87202dd64d402c2e → duplicateFolder
NB, · & → are whitespace [space] and [tab]
Next, edit the text, removing the offending lines, and save with Unix-style endings (ie only LF, not CRLF). With this example, we make this:
040000·tree·4ad0d8ef014b8cc09c95694399254eff43217bfb → EXT
040000·tree·d65085e4a05ea9ac8b79e37b87202dd64d402c2e → duplicateFolder
040000·tree·fd0661d698ace91135a8473b26707892b7c89c32 → ToolTester
040000·tree·3cdcc756ee0ed636c44828927126911d0ab28a18 → xNotAlphabetic
Type cat tmpfile.txt | git mktree
which will make a new, fixed tree object and save it, and return the new hash:
a55115e4a05ea9ac8b79e37b872024d64d4r2c2e
a.k.a. for demo purposes new_tree_hash
Next git replace will create a new reference, which forces all previously incident links to use the new, fixed object instead.
git replace bad_tree_hash new_tree_hash
This will solve your immediate problem. If you're interested, look at the overriding link in the .git/refs/replace
folder.
The bad tree object will continue to generate warnings whenever you do a check on your repository with git fsck
, but it can be ignored, and all your commits and other links will be consistent and working regardless.
8 year retrospective: There's probably a way to just delete the old, corrupt tree since git replace
should make it moot.
Further warning:
This hack could also be rejected by a git service eg BitBucket or GitHub, since they could view it as corruption.