如何修复损坏的git存储库? [英] How to fix corrupted git repository?

查看:3015
本文介绍了如何修复损坏的git存储库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把我的repo文件夹放在我的ubuntu的一个文件夹中,并放在一台新机器上,我得到了这个:

  christopher @ christopher-laptop:〜/ source / personal $ git clone〜/ Ubuntu \ One \ Side \ Work / projects.git / 
克隆到'projects'中
完成。
致命:无法读取树29a422c19251aeaeb907175e9b3219a9bed6c616
christopher @ christopher-laptop:〜/ source / personal

所以我试着查看了许多这样的问题,这些问题在这里已经被问到,其中大多数人说要运行 git fsck --full ,然后我当我尝试的时候得到这个。

  christopher @ christopher-laptop:〜/ Ubuntu One Side Work / projects.git $ git fsck --full 
检查对象目录:100%(256/256),完成。
检查对象:100%(447/447),完成。从提交235ae1f48701d577d71ebd430344a159e5ba4881

损坏的链接提交从树上632a9cf0ef9fccea08438b574e2f1c954f4ff08b
984c11abfc9c2839b386f29c574d9e03383fa589
断链成团块25a742dff0a403b2b3884f2ffddf63eb45721fac从树632a9cf0ef9fccea08438b574e2f1c954f4ff08b

断链成团块dd4e97e22e159a585b20e21028f964827d5afa4e
从树632a9cf0ef9fccea08438b574e2f1c954f4ff08b
到树29a422c19251aeaeb907175e9b3219a9bed6c616
损坏的链接,以树8084e8e04d510cc28321f30a9646477cc50c235c
断开的链接断开的链接从树632a9cf0ef9fccea08438b574e2f1c954f4ff08b
。从树774b5b4157b4caae1c6cad96c8eaf5d4eba2c628
成团块a0daa0c1567b55d8de2b4d7a3bc010f58c047eab
从树上断开的链接774b5b4157b4caae1c6cad96c8eaf5d4eba2c628
到blob e9052d35bfb6d30065b206fc43f4200a04d5281b
b从树774b5b4157b4caae1c6cad96c8eaf5d4eba2c628
roken链接成团块从树上4aa336dc1a5838e8918e03b85580069d83f4ad09
从树上e5674a91a53e15575a1f3bf5786bc5cc719fb483
树8cc55ec952dc192a233e062201d1e7e873ac3db0
断开链接1a3a5e4dd2502ac121c22f743c4250e254a94eeb
断链成团块4a994e1e7bb7ce28dcec98bad48b9a891d7dec51
断链从树e5674a91a53e15575a1f3bf5786bc5cc719fb483
成团块从树上252ab84542264e1589576b6ee51e7a31e580a0e2
到树2069041cd5950e529e2991d37b7290ec021d90d4
断开链接ac033bf9dc846101320c96a5ce8aceb8c96ec098
断开的链接从树2d4964aa4d4f5d8c7228518ce72ef6a63f820c6d
成团块从树上d83690e1b9a6bdd8a08754b38231799acefcb2ab
断链c7192e82fc581bd6448bda1a25e8729bdac5f4ff
到blob 30d54d47ae82add1917ca173d42e58b396df580b
破碎从树7c66306901fc71389623286936cef172d4ffe408
链接成团块bc7e05d705401273b1df4e939de0f540597c0931从树0940f5fd227d4c84d6e6749d872db50a4522ae3a
到树923767594ac22023e824948d65622fe5b407d1a1
断开的链接
断开的链接从树8eadcd2a971e8357d24f0d80f993d2963452209f
成团块2598bde3dc8cb80ee49510b8159344004b88645f
从断开的链接树ffa302dd0d969172ef23caeefe856ab2f57a4e4d
成团块从树上7045b8870a49ce30a2027537a96d73d162bda773
d6925fa431be1ac585bf9a481e98f75107a6e6fb
断链成团块25688652dea26f61f576ca1b52b9d1a18fbfd01d从树37e4705d34bd440ce681ae32ae9a180a13256d72
到树246f564d4cee53339b8a4244f3173b61caa518eb

断开链接丢失一滴d6925fa431be1ac585bf9a481e98f75107a6e6fb
缺少blob ac033bf9dc846101320c96a5ce8aceb8c96ec098
缺少树29a422c19251aeaeb907175e 9b3219a9bed6c616
缺少树8084e8e04d510cc28321f30a9646477cc50c235c
丢失一滴30d54d47ae82add1917ca173d42e58b396df580b
缺少树8cc55ec952dc192a233e062201d1e7e873ac3db0
丢失一滴e9052d35bfb6d30065b206fc43f4200a04d5281b
叼着树4b26e95db542c72ac4a22ec25abe38fb2de79752
丢失一滴d83690e1b9a6bdd8a08754b38231799acefcb2ab
丢失一滴25a742dff0a403b2b3884f2ffddf63eb45721fac
缺少树923767594ac22023e824948d65622fe5b407d1a1
丢失一滴25688652dea26f61f576ca1b52b9d1a18fbfd01d
丢失一滴2598bde3dc8cb80ee49510b8159344004b88645f
叼着树3a683869f1bb0c1634de75700c316b3b36570dbd
悬挂一滴4098d30843380d798a811f1aa9a02994f0dbbb27
缺少树2069041cd5950e529e2991d37b7290ec021d90d4
丢失一滴4a994e1e7bb7ce28dcec98bad48b9a891d7dec51
缺少blob 1a3a5e4dd2502ac121c22f743c4250e254a94eeb
缺少blob a0daa0c1567b55d8de2b4d7a3bc010f58c047eab
悬挂树6c7b5162aa7a303fa 3fe8dc393c5da564e309521
缺失提交984c11abfc9c2839b386f29c574d9e03383fa589
缺失的斑点bc7e05d705401273b1df4e939de0f540597c0931
缺失的斑点dd4e97e22e159a585b20e21028f964827d5afa4e
缺失树246f564d4cee53339b8a4244f3173b61caa518eb
悬空提交a01f5c1e5315dc837203d6dee00d3493be9c5db9

看起来很糟糕。当我做一个 git log |头部我得到这个

  christopher @ christopher-laptop:〜/ Ubuntu One Side Work / projects。 git $ git log |头部
错误:无法读取984c11abfc9c2839b386f29c574d9e03383fa589
致命:无法遍历提交的父母235ae1f48701d577d71ebd430344a159e5ba4881
提交2fb0d2d0643b445440f01b164f11ee9ee71fca48
作者:christopher< christopher@christopher.christopher>
日期:8月7日星期三15:51:42 2013 -0400

完成第7章

这里的其他问题已经说过要看 ./git / refs / heads / master 。这是一个裸仓库,并且 refs / heads / 存在,但是 refs / heads / master 没有。 HEAD在裸仓库中显示 ref:refs / heads / master 尽管

packed-

 #pack-refs with:peeled 
2fb0d2d0643b445440f01b164f11ee9ee71fca48参考文献/ refs
does not say this though

head / master

还有其他问题提示运行 git reflog ,当我运行时没有输出显示。



所以我真的不知道该怎么做。应该采取什么策略?



编辑:



 提交996e03b949aea176238e3c7a8452700bbb987ac9 
作者:克里斯托弗< christopher @ christopher>
日期:Wed Jul 3 23:00:44 2013 -0400

许多更改
错误:无法读取984c11abfc9c2839b386f29c574d9e03383fa589
致命:未能遍历提交的父母235ae1f48701d577d71ebd430344a159e5ba4881

这似乎阻止了git prune的正常工作

解决方案

TL; DR



Git没有按照您的想法存储历史记录。它根据祖先链在运行时计算历史记录。如果您的祖先缺少blob,树木或提交,那么您可能无法完全恢复您的历史记录。



从备份还原丢失的对象



您可以尝试的第一件事是从备份中还原丢失的项目。例如,查看您是否具有以 .git / objects / 98 / 4c11abfc9c2839b386f29c574d9e03383fa589 存储的提交的备份。如果是这样,你可以恢复它。



你也可以看看 git-verify-pack git-unpack-objects



手术切除



如果您无法从备份中替换缺失的项目,您可能可以删除缺失的历史记录。例如,您可能会检查历史记录或引用日志以查找提交984c11abfc9c2839b386f29c574d9e03383fa589的祖先。如果你发现一个完整的,那么:


  1. 将你的Git工作目录复制到某个临时目录。

  2. 将硬重置为未损坏的提交。

  3. 将当前文件复制回Git工作树,但请确保不要将.git文件夹复制回来!
  4. >
  5. 提交当前的工作树,并尽力将其视为所有缺失历史记录的压扁提交。

如果有效,你当然会失去干预的历史。此时,如果您有工作历史日志,那么剪掉所有无法访问的提交和对象的历史记录和reflog是个不错的主意。



完全恢复和重新创建记录,初始化



如果您的存储库仍然损坏,那么希望您有一个可以从中恢复的未损坏的备份或克隆。如果没有,但你当前的工作目录包含有效的文件,那么你总是可以重新初始化Git。例如:

  rm -rf .git 
git init
git add。
git commit -m'重新初始化没有旧历史记录的版本库'

但如果您的存储库历史记录确实无法恢复,它可能是您唯一的选择。 YMMV。


I tied cloning my repo which I keep on my ubuntu one folder to a new machine and I got this:

christopher@christopher-laptop:~/source/personal$ git clone ~/Ubuntu\ One\ Side\ Work/projects.git/
Cloning into 'projects'...
done.
fatal: unable to read tree 29a422c19251aeaeb907175e9b3219a9bed6c616
christopher@christopher-laptop:~/source/personal$ 

So I tried looking at the many other questions like this that have been asked here and most of them say to run git fsck --full and then I get this when I try that.

christopher@christopher-laptop:~/Ubuntu One Side Work/projects.git$ git fsck --full
Checking object directories: 100% (256/256), done.
Checking objects: 100% (447/447), done.
broken link from  commit 235ae1f48701d577d71ebd430344a159e5ba4881
              to  commit 984c11abfc9c2839b386f29c574d9e03383fa589
broken link from    tree 632a9cf0ef9fccea08438b574e2f1c954f4ff08b
              to    blob 25a742dff0a403b2b3884f2ffddf63eb45721fac
broken link from    tree 632a9cf0ef9fccea08438b574e2f1c954f4ff08b
              to    blob dd4e97e22e159a585b20e21028f964827d5afa4e
broken link from    tree 632a9cf0ef9fccea08438b574e2f1c954f4ff08b
              to    tree 29a422c19251aeaeb907175e9b3219a9bed6c616
broken link from    tree 632a9cf0ef9fccea08438b574e2f1c954f4ff08b
              to    tree 8084e8e04d510cc28321f30a9646477cc50c235c
broken link from    tree 774b5b4157b4caae1c6cad96c8eaf5d4eba2c628
              to    blob a0daa0c1567b55d8de2b4d7a3bc010f58c047eab
broken link from    tree 774b5b4157b4caae1c6cad96c8eaf5d4eba2c628
              to    blob e9052d35bfb6d30065b206fc43f4200a04d5281b
broken link from    tree 774b5b4157b4caae1c6cad96c8eaf5d4eba2c628
              to    blob 1a3a5e4dd2502ac121c22f743c4250e254a94eeb
broken link from    tree 4aa336dc1a5838e8918e03b85580069d83f4ad09
              to    tree 8cc55ec952dc192a233e062201d1e7e873ac3db0
broken link from    tree e5674a91a53e15575a1f3bf5786bc5cc719fb483
              to    blob 4a994e1e7bb7ce28dcec98bad48b9a891d7dec51
broken link from    tree e5674a91a53e15575a1f3bf5786bc5cc719fb483
              to    blob ac033bf9dc846101320c96a5ce8aceb8c96ec098
broken link from    tree 252ab84542264e1589576b6ee51e7a31e580a0e2
              to    tree 2069041cd5950e529e2991d37b7290ec021d90d4
broken link from    tree 2d4964aa4d4f5d8c7228518ce72ef6a63f820c6d
              to    blob d83690e1b9a6bdd8a08754b38231799acefcb2ab
broken link from    tree c7192e82fc581bd6448bda1a25e8729bdac5f4ff
              to    blob 30d54d47ae82add1917ca173d42e58b396df580b
broken link from    tree 7c66306901fc71389623286936cef172d4ffe408
              to    blob bc7e05d705401273b1df4e939de0f540597c0931
broken link from    tree 0940f5fd227d4c84d6e6749d872db50a4522ae3a
              to    tree 923767594ac22023e824948d65622fe5b407d1a1
broken link from    tree 8eadcd2a971e8357d24f0d80f993d2963452209f
              to    blob 2598bde3dc8cb80ee49510b8159344004b88645f
broken link from    tree ffa302dd0d969172ef23caeefe856ab2f57a4e4d
              to    blob d6925fa431be1ac585bf9a481e98f75107a6e6fb
broken link from    tree 7045b8870a49ce30a2027537a96d73d162bda773
              to    blob 25688652dea26f61f576ca1b52b9d1a18fbfd01d
broken link from    tree 37e4705d34bd440ce681ae32ae9a180a13256d72
              to    tree 246f564d4cee53339b8a4244f3173b61caa518eb
missing blob d6925fa431be1ac585bf9a481e98f75107a6e6fb
missing blob ac033bf9dc846101320c96a5ce8aceb8c96ec098
missing tree 29a422c19251aeaeb907175e9b3219a9bed6c616
missing tree 8084e8e04d510cc28321f30a9646477cc50c235c
missing blob 30d54d47ae82add1917ca173d42e58b396df580b
missing tree 8cc55ec952dc192a233e062201d1e7e873ac3db0
missing blob e9052d35bfb6d30065b206fc43f4200a04d5281b
dangling tree 4b26e95db542c72ac4a22ec25abe38fb2de79752
missing blob d83690e1b9a6bdd8a08754b38231799acefcb2ab
missing blob 25a742dff0a403b2b3884f2ffddf63eb45721fac
missing tree 923767594ac22023e824948d65622fe5b407d1a1
missing blob 25688652dea26f61f576ca1b52b9d1a18fbfd01d
missing blob 2598bde3dc8cb80ee49510b8159344004b88645f
dangling tree 3a683869f1bb0c1634de75700c316b3b36570dbd
dangling blob 4098d30843380d798a811f1aa9a02994f0dbbb27
missing tree 2069041cd5950e529e2991d37b7290ec021d90d4
missing blob 4a994e1e7bb7ce28dcec98bad48b9a891d7dec51
missing blob 1a3a5e4dd2502ac121c22f743c4250e254a94eeb
missing blob a0daa0c1567b55d8de2b4d7a3bc010f58c047eab
dangling tree 6c7b5162aa7a303fa3fe8dc393c5da564e309521
missing commit 984c11abfc9c2839b386f29c574d9e03383fa589
missing blob bc7e05d705401273b1df4e939de0f540597c0931
missing blob dd4e97e22e159a585b20e21028f964827d5afa4e
missing tree 246f564d4cee53339b8a4244f3173b61caa518eb
dangling commit a01f5c1e5315dc837203d6dee00d3493be9c5db9

That looks really bad. When I do a git log | head I get this

christopher@christopher-laptop:~/Ubuntu One Side Work/projects.git$ git log | head
error: Could not read 984c11abfc9c2839b386f29c574d9e03383fa589
fatal: Failed to traverse parents of commit 235ae1f48701d577d71ebd430344a159e5ba4881
commit 2fb0d2d0643b445440f01b164f11ee9ee71fca48
Author: christopher <christopher@christopher.christopher>
Date:   Wed Aug 7 15:51:42 2013 -0400

    finishing chapter 7

Other questions here have said to look at ./git/refs/heads/master. It's a bare repo and refs/heads/ exists but refs/heads/master does not. HEAD in the bare repo says ref: refs/heads/master though

packed-refs does say this though

# pack-refs with: peeled 
2fb0d2d0643b445440f01b164f11ee9ee71fca48 refs/heads/master

Still other questions have suggested running git reflog and no output shows up when I run that.

So I really have no idea what to do here. What strategy should be taken? Is it possible to reset head to this last commit on Aug 7

EDIT:

Doing a git log and going to the bottom of the screen output shows this:

commit 996e03b949aea176238e3c7a8452700bbb987ac9
Author: christopher <christopher@christopher>
Date:   Wed Jul 3 23:00:44 2013 -0400

    many many changes
error: Could not read 984c11abfc9c2839b386f29c574d9e03383fa589
fatal: Failed to traverse parents of commit 235ae1f48701d577d71ebd430344a159e5ba4881

That seems to be preventing the git prune from working

解决方案

TL;DR

Git doesn't really store history the way you think it does. It calculates history at run-time based on an ancestor chain. If your ancestry is missing blobs, trees, or commits then you may not be able to fully recover your history.

Restore Missing Objects from Backups

The first thing you can try is to restore the missing items from backup. For example, see if you have a backup of the commit stored as .git/objects/98/4c11abfc9c2839b386f29c574d9e03383fa589. If so you can restore it.

You may also want to look into git-verify-pack and git-unpack-objects in the event that the commit has already been packed up and you want to return it to a loose object for the purposes of repository surgery.

Surgical Resection

If you can't replace the missing items from a backup, you may be able to excise the missing history. For example, you might examine your history or reflog to find an ancestor of commit 984c11abfc9c2839b386f29c574d9e03383fa589. If you find one intact, then:

  1. Copy your Git working directory to a temporary directory somewhere.
  2. Do a hard reset to the uncorrupted commit.
  3. Copy your current files back into the Git work tree, but make sure you don't copy the .git folder back!
  4. Commit the current work tree, and do your best to treat it as a squashed commit of all the missing history.

If it works, you will of course lose the intervening history. At this point, if you have a working history log, then it's a good idea to prune your history and reflogs of all unreachable commits and objects.

Full Restores and Re-Initialization

If your repository is still broken, then hopefully you have an uncorrupted backup or clone you can restore from. If not, but your current working directory contains valid files, then you can always re-initialize Git. For example:

rm -rf .git
git init
git add .
git commit -m 'Re-initialize repository without old history.'

It's drastic, but it may be your only option if your repository history is truly unrecoverable. YMMV.

这篇关于如何修复损坏的git存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆