How to clean damaged objects in git repository?

1.6k Views Asked by At

How to get a clean git repository? This can be with losing some history but not all of it though. This repository could have been corrupted by a concurrent modification and invalid merging by the SyncThing tool (a tool to synchronize files in machines).

On every git commit:

...
Could not read e7ac6796b130b61f0f382b0d31845239eeb61e6e
fatal: Failed to traverse parents of commit b5e5b4601495191a5700bcba4e2227515db77376
fatal: failed to run repack
...

On git fsck --full

Checking object directories: 100% (256/256), done.
Checking objects: 100% (26828/26828), done.
broken link from  commit b5e5b4601495191a5700bcba4e2227515db77376
              to  commit e7ac6796b130b61f0f382b0d31845239eeb61e6e
broken link from    tree f2678f1595a2eebd798afb9496614a09eb75d1b6
              to    tree ecd48fc55a31781d2b4cbad0629c10590de0b1a5
broken link from    tree 1259b1ca62274228c9c42aaf4a9a2276a5b60a32
              to    blob 00162d2c6425ae3166187089ff2724cb2bce23db
dangling commit f5079404103ce1343d46884219cbea7d73d0e849
dangling blob b03a78f393908690b5a395b2842c2d99b1d185e7
dangling blob 134bba35a1bda0ed2c4e57640738d8d0e999e483
dangling blob 1180e69eaaa1915c12a4c1d77b20bbc08ad60633
dangling commit f39624880f4f97131d1d56deb7a3ba5289186015
dangling commit 27d46e5a9fc0d28eb7b5229cb9ca06cbf1d6d065
missing blob 00162d2c6425ae3166187089ff2724cb2bce23db
dangling commit 5e2a2d64abce8e24e83f1d5c59f096b6c7180959
dangling commit 273b659b039d66014957dbe1fa3eb975e80b9d03
dangling commit 084d6f70f7b72e1ee4bfd181ec00f8840b9abe7a
dangling commit 5657bf6382d23a2c216af7364612ef73931ad88a
dangling blob cb6bd151bdfe88348d248437b8ddb77eb4669cab
dangling blob aa7ea55c9a797f1ccb1285c1a2fdf81c7fbcacf5
missing commit e7ac6796b130b61f0f382b0d31845239eeb61e6e
missing tree ecd48fc55a31781d2b4cbad0629c10590de0b1a5
dangling commit f1f62b99255d06356be53666bfac72a67b93f264

On git gc:

error: Could not read e7ac6796b130b61f0f382b0d31845239eeb61e6e
fatal: Failed to traverse parents of commit b5e5b4601495191a5700bcba4e2227515db77376
fatal: failed to run repack

On a try to clone this repository:

> git init 
> git remote add parent <mypath to the broken repo>
... 
> git pull parent master
remote: error: Could not read e7ac6796b130b61f0f382b0d31845239eeb61e6e
remote: fatal: Failed to traverse parents of commit b5e5b4601495191a5700bcba4e2227515db77376
error: git upload-pack: git-pack-objects died with error.
fatal: git upload-pack: aborting due to possible repository corruption on the remote side.
remote: aborting due to possible repository corruption on the remote side.
fatal: protocol error: bad pack header

git version 2.25.1

2

There are 2 best solutions below

0
On

I managed to replace the parent for the broken commit link as below. It now runs git log , git blame and other which use the replacements. But git fsck still shows the error. Nevertheless I can use git now to this extent. I found this advice at this post.

So first I checked dates for all dangling commits:

git show <my dangling commits shas>

Then I replaced the parent for the broken commit with the earliest:

git replace --graft b5e5b4601495191a5700bcba4e2227515db77376 f1f62b99255d06356be53666bfac72a67b93f264
6
On

It is better to:

That being said, using git bundle would be safer, as I mentioned before.

The OP Artyom prefers in the comments an archive-based option:

tar --exclude-from=$backupexcludefile \
      --xattrs --create --preserve-permissions --file - ~ | \
    7za a -si -mx=3 $backupfile

The idea remains the same: synchronize only one file.