如何清理git存储库中损坏的对象?

如何清理git存储库中损坏的对象?,git,syncthing,Git,Syncthing,如何获得一个干净的git存储库?这可能是因为失去了一些历史,但不是全部。此存储库可能已被工具(用于同步计算机中文件的工具)的并发修改和无效合并损坏 每次提交git时: ... Could not read e7ac6796b130b61f0f382b0d31845239eeb61e6e fatal: Failed to traverse parents of commit b5e5b4601495191a5700bcba4e2227515db77376 fatal: failed to run

如何获得一个干净的git存储库?这可能是因为失去了一些历史,但不是全部。此存储库可能已被工具(用于同步计算机中文件的工具)的并发修改和无效合并损坏

每次提交
git时

...
Could not read e7ac6796b130b61f0f382b0d31845239eeb61e6e
fatal: Failed to traverse parents of commit b5e5b4601495191a5700bcba4e2227515db77376
fatal: failed to run repack
...
在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
在git gc上的

error: Could not read e7ac6796b130b61f0f382b0d31845239eeb61e6e
fatal: Failed to traverse parents of commit b5e5b4601495191a5700bcba4e2227515db77376
fatal: failed to run repack
在尝试克隆此存储库时:

> 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 init
>git远程添加父级
... 
>git拉母版
远程:错误:无法读取e7ac6796b130b61f0f382b0d31845239eeb61e6e
远程:致命:未能遍历提交B5E5B4601495191A5700BCBA4E222751DB77376的父级
错误:git upload pack:git pack对象因错误而死亡。
致命:git上载包:由于远程端可能存在存储库损坏而中止。
远程:由于远程端可能存在存储库损坏而中止。
致命:协议错误:错误的包头
git版本2.25.1

最好:

  • 通过恢复较旧的
    .stversions
  • 验证此旧版本是否有效(,根据中的建议,和)
  • 尝试并重做当前损坏的repo中的文件,以获取最新的源
  • 添加、提交和推送这些更改
也就是说,正如我所说,使用git捆绑包会更安全

首选基于存档的选项:


想法是一样的:只同步一个文件。

据我所知,您唯一的选择是找到存储库的干净、可用的副本。如果幸运的话-SyncThing可能保存了冲突文件。SyncThing制作了冲突文件的副本,但我没有看到这些git对象的冲突文件:
find-name'*-conflict*”
没有给出任何信息。检查了
.stversions
文件夹和“Restore Version”gui。我没有.git文件夹,也没有它的文件。不过还有其他一些文件。断开的提交是5个月前的事了(我很久都不在乎这个消息了)。所以不要认为SyncThing能帮上忙。我有那时候的备份。希望回购协议在那里还可以。因此,我将尝试对其重新进行提交。@Artyom当然,使用备份是一个不错的选择。但是,同样,通过一个捆绑包(一个文件)而不是所有文件来同步Git存储库更好。我使用这个.git repo在我的home dir上执行
tar--exclude from=$backupexcludefile--xattrs--create--preserve permissions--file--7za a-si-mx=3$backupfile
git bundle
对我来说是一个复杂的解决方案,因为它是git的“本地”解决方案,我想为我的私人备份提供一个快速而简单的解决方案。@Artyom说得对。我已将您的评论包含在答案中以提高可视性。警告:
git status
(与
git不同,没有可选的锁status
)将在发现任何更改时尝试修改索引。根据数据恢复最佳实践,最好避免执行任何可能会写回您正在从中恢复数据的损坏系统的操作。(我希望Git在出现腐败时跳过这一步,但最好不要冒险。)当然,在副本上这样做是安全的。
tar --exclude-from=$backupexcludefile \
      --xattrs --create --preserve-permissions --file - ~ | \
    7za a -si -mx=3 $backupfile