你如何在git仓库中恢复损坏的对象(对于新手)? [英] How do you restore a corrupted object in a git repository (for newbies)?

查看:142
本文介绍了你如何在git仓库中恢复损坏的对象(对于新手)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天尝试打开我的存储库,它没有提交历史记录。我试过的所有东西(git status,git log,git checkout ...)都抛出了一个有关损坏对象的错误。



我在网上研究了这个问题,发现文章 by Linus Torvalds,但在找到断开的链接ID的时候丢失了:我的文件ID,树或blob都没有匹配错误消息引发的罪魁祸首。



I然后返回到文章恢复git objects damaged by硬盘故障和(在将罪魁祸首移开之后)一直工作,直到

  $ cat packed-refs 

我的电脑说: cat:packed- refs:没有这样的文件或目录
我跳过了这一步并做了

  $ G它fsck  - 完全

并且得到适当的输出,但是我应该复制罪魁祸首或者我所说的罪魁祸首,即错误抛出的sha1 ID)从备份存储库返回到主存储库,然后将缺失的对象从备份存储库复制到主存储库中,据我所知;我也不想做任何事情,我也可能会迫使我以后不能解除压力。



所以我的问题(s )是否是,我应该做了一个备份( ooh,newbie alert ),或者当我解压缩.pack文件时发生了什么?并且是我抄袭的罪魁祸首实际上是一个干净的文件,即没有损坏?



(我认为这只是公平的告诉你,我最初感到困惑在git和fsck之间的Torvalds文件中是一个简单的破折号。所以我真的很新鲜。)



BUG -LIST



原始错误:



  $ git status 
致命:对象016660b7605cfc2da85f631bbe809f7cb7962608已损坏



移动损坏对象后的错误:



  $ git status 
致命错误:bad object HEAD
$ git fsck --full
错误:HEAD:无效的sha1指针016660b7605cfc2da85f631bbe809f7cb7962608
错误:refs / heads / RPG不指向有效的对象!
悬挂树2c1033501b82e301d47dbf53ba0a199003af25a8
悬挂一滴531aca5783033131441ac7e132789cfcad82d06d
晃来晃去的blob 74a47ff40a8c5149a8701c2f4b29bba408fa36f5
悬挂一滴b8df4d9751c0518c3560e650b21a182ea6d7bd5e
悬挂一滴fc2d15aead4bd0c197604a9f9822d265bb986d8b

$ git的LS-树2c1033501b82e301d47dbf53ba0a199003af25a8
040000树4a8b0b3747450085b1cd920c22ec82c18d9311bd文件夹1
040000树33298295f646e8b378299191ce20b4594f5eb625文件夹2
040000树dec82bad6283fc7fcc869c20fdea9f8588a2f1b2 folder3
040000树4544967c6b04190f4c95b516ba8a86cab266a872 folder4

$ git的LS-树dec82bad6283fc7fcc869c20fdea9f8588a2f1b2
100644团块67bda6df733f6cd76fc0fc4c8a6132d8015591d8的fileA
100644一滴4cb7272c9e268bfbd83a04e568d7edd87f78589c FILEB
100644一滴ce9e0f2cc4d3b656fa30340afbdfed47fe35f3ef fileC

$ git的LS-树4544967c6b04190f4c95b516ba8a86cab266a872
100644一滴d64fe3ad d8328d81b1f31c9dbd528956ab391fb6申请
100644一滴d1ebd7df7082abc5190d87caa821bf3edb7b68e8 fileE
100644一滴bb6cd264e47a3e5bc7beadf35ea13bac86024b02 ...
100644一滴995d622b9012f4ef69921091d1e1a73f32aa94e6
100644一滴9141dbd2b1c7931a6461195934b6599f5dfb485a
100644一滴ab128da1d82907cd0568448dc089a7996d5f79d3
100644一滴57b11a7eb408a79739d2bb60a0dc35c591340d18
100644一滴118105291c1c6ca4a01744889ffafbb018bc7ed3
100644一滴86b1dfda56d0603f16910228327751f869d16bdc
100644一滴077fe0cddde0d0be9d0974f928f66815caca7b76
100644一滴c0b32fd0450f21994bdc53ea83d3cf0bccd74004
100644一滴37b87a4d11453468c4ae04572db5d322cd2d1d80
100644一滴79d39f8d4e57fa3a71664598a63b6dfd88149638
100644 BLOB ee07bbe3e8cb5d6bb79fb0cd52cfbc9bd830498d文件

$ git ls-tree 33298295f646e8b378299191ce20b4594f5eb625
100644 blob f9d6f45cd028aec97f761f00c5f4f2f6b50fb925 MoreFiles
100644 blob 0cb9eed1d0dd9214d54a03af 1bda21f37b8c0d02
100644团块198e4f97ece735cce47b7e99b54f1b5fa99fabf5
100644团块fc004212fa8e483e5a8ab35b508027c7a9a1cbfa
100644团块0c7d74c7a9a8337b4a9f20802b63d71d42287f89

$ GIT中LS-树4a8b0b3747450085b1cd920c22ec82c18d9311bd
100644团块0320f5b23dd7cce677fac60b9ad03f418cff5c88 oneLASTfile



移动损坏的对象后:



  $ git log --raw --all 
致命错误:object 016660b7605cfc2da85f631bbe809f7cb7962608已损坏

$ cat packed-refs
cat:packed-refs:没有这样的文件或目录

$ git fsck --full
致命:object 016660b7605cfc2da85f631bbe809f7cb7962608已损坏



将文件移出后:



  $ git fsck --full` 
错误:HEAD:无效的sha1指针016660b7605cfc2da85f631bbe809f7cb7962608
错误:refs / heads / RPG不指向有效的对象!
悬空树2c1033501b82e301d47dbf53ba0a199003af25a8
悬空团块531aca5783033131441ac7e132789cfcad82d06d
悬空团块74a47ff40a8c5149a8701c2f4b29bba408fa36f5
悬空团块b8df4d9751c0518c3560e650b21a182ea6d7bd5e
悬空团块fc2d15aead4bd0c197604a9f9822d265bb986d8b



解包.pack文件后:



  $ git log 
致命:坏对象HEAD

$ cat packed-refs
cat:packed-refs:没有这样的文件或目录

$ git fsck --full
错误:HEAD:无效的sha1指针016660b7605cfc2da85f631bbe809f7cb7962608
错误:refs / heads / RPG不指向有效的对象!
悬空树2c1033501b82e301d47dbf53ba0a199003af25a8
悬空团块531aca5783033131441ac7e132789cfcad82d06d
悬空团块74a47ff40a8c5149a8701c2f4b29bba408fa36f5
悬空团块b8df4d9751c0518c3560e650b21a182ea6d7bd5e
悬空团块fc2d15aead4bd0c197604a9f9822d265bb986d8b


解决方案

好的,那么。我们可以从第二个错误消息中看到,您移动的损坏对象是提交。 (HEAD指着它!)不幸的是,这意味着很难手动修复它。 (除非你确切地记得提交信息是什么以及你什么时候提交,否则我认为这很难做到。)幸运的是,这确实意味着使用相同的文件内容重新生成一个新的提交很容易 - 你会只需要为它写一条新消息。



在你开始之前,看看 .git / HEAD - 如果它是一个分支名称,请记住以后。



首先,我们需要确定此提交的父项应该是什么。您可以使用 git reflog 来查看HEAD的reflog,并在您提交016660b之前找到HEAD所在的SHA1。它应该看起来像这样:

  016660b HEAD @ {n}:commit:< subject of commit> 
1234abc HEAD @ {n-1}:...

您可以复制SHA1并检查出提交:

  git checkout 1234abc 



然后,您可以在树中读取您的损坏提交:

  git read-tree 2c1033501b82e301d47dbf53ba0a199003af25a8 

然后提交!

  git commit 

现在,这里有一些关于你的分支应该发生什么的问题。如果HEAD指向一个分支(称为master),而该分支又指向损坏的提交,那么我们肯定希望解决这个问题:

  git branch -d master#删除原来的主分支
git checkout -b master#在这里重新创建

如果其他分支包含损坏的提交,那么您也必须对它们进行一些恢复 - 请告诉我您是否需要帮助。


I tried to open my repository today and it came up with no commit history. Everything I tried (git status, git log, git checkout...) threw an error about a corrupt object.

I researched this problem online and found the article by Linus Torvalds, but got lost at the point where he found the broken link ID: none of my file IDs, tree or blob, match the culprit ID thrown by the error message.

I then returned to the article on recovering "git objects damaged by hard disk failure" and (after moving the culprit object out of the way) worked my way through until

$ cat packed-refs

at which point my computer said: cat: packed-refs: No such file or directory I skipped that step and did the

$ git fsck --full

and got the appropriate output, but then I was supposed to copy the culprit (or what I was referring to as the culprit, the sha1 ID thrown by the error) from a backup repository back into the main repository, then copy the missing objects from the backup repository into the main repository, as far as I can tell; and I don't want to do anything too drastic or I might force something I can't unforce later.

So my question(s) is (are), was I supposed to have made a backup (ooh, newbie alert), or was that what happened when I unpacked the .pack file? And is the "culprit" I'm copying back actually a clean file, i.e. not corrupted?

(I think it only fair to tell you that I was initially confused by a simple dash in Torvalds' file between the "git" and "fsck." So I'm REALLY new at this.)

BUG-LIST

Original bug:

$ git status
fatal: object 016660b7605cfc2da85f631bbe809f7cb7962608 is corrupted

Bug after moving corrupt object:

$ git status
fatal: bad object HEAD
$ git fsck --full
error: HEAD: invalid sha1 pointer 016660b7605cfc2da85f631bbe809f7cb7962608
error: refs/heads/RPG does not point to a valid object!
dangling tree 2c1033501b82e301d47dbf53ba0a199003af25a8
dangling blob 531aca5783033131441ac7e132789cfcad82d06d
dangling blob 74a47ff40a8c5149a8701c2f4b29bba408fa36f5
dangling blob b8df4d9751c0518c3560e650b21a182ea6d7bd5e
dangling blob fc2d15aead4bd0c197604a9f9822d265bb986d8b

$ git ls-tree 2c1033501b82e301d47dbf53ba0a199003af25a8
040000 tree 4a8b0b3747450085b1cd920c22ec82c18d9311bd    folder1
040000 tree 33298295f646e8b378299191ce20b4594f5eb625    folder2
040000 tree dec82bad6283fc7fcc869c20fdea9f8588a2f1b2    folder3
040000 tree 4544967c6b04190f4c95b516ba8a86cab266a872    folder4

$ git ls-tree dec82bad6283fc7fcc869c20fdea9f8588a2f1b2
100644 blob 67bda6df733f6cd76fc0fc4c8a6132d8015591d8    fileA
100644 blob 4cb7272c9e268bfbd83a04e568d7edd87f78589c    fileB
100644 blob ce9e0f2cc4d3b656fa30340afbdfed47fe35f3ef    fileC

$ git ls-tree 4544967c6b04190f4c95b516ba8a86cab266a872
100644 blob d64fe3add8328d81b1f31c9dbd528956ab391fb6    fileD
100644 blob d1ebd7df7082abc5190d87caa821bf3edb7b68e8    fileE
100644 blob bb6cd264e47a3e5bc7beadf35ea13bac86024b02    ...
100644 blob 995d622b9012f4ef69921091d1e1a73f32aa94e6
100644 blob 9141dbd2b1c7931a6461195934b6599f5dfb485a 
100644 blob ab128da1d82907cd0568448dc089a7996d5f79d3
100644 blob 57b11a7eb408a79739d2bb60a0dc35c591340d18
100644 blob 118105291c1c6ca4a01744889ffafbb018bc7ed3
100644 blob 86b1dfda56d0603f16910228327751f869d16bdc
100644 blob 077fe0cddde0d0be9d0974f928f66815caca7b76
100644 blob c0b32fd0450f21994bdc53ea83d3cf0bccd74004
100644 blob 37b87a4d11453468c4ae04572db5d322cd2d1d80
100644 blob 79d39f8d4e57fa3a71664598a63b6dfd88149638
100644 blob ee07bbe3e8cb5d6bb79fb0cd52cfbc9bd830498d    files

$ git ls-tree 33298295f646e8b378299191ce20b4594f5eb625
100644 blob f9d6f45cd028aec97f761f00c5f4f2f6b50fb925    MoreFiles
100644 blob 0cb9eed1d0dd9214d54a03af1bda21f37b8c0d02
100644 blob 198e4f97ece735cce47b7e99b54f1b5fa99fabf5
100644 blob fc004212fa8e483e5a8ab35b508027c7a9a1cbfa
100644 blob 0c7d74c7a9a8337b4a9f20802b63d71d42287f89

$ git ls-tree 4a8b0b3747450085b1cd920c22ec82c18d9311bd
100644 blob 0320f5b23dd7cce677fac60b9ad03f418cff5c88    oneLASTfile

After moving the corrupted object back:

$ git log --raw --all
fatal: object 016660b7605cfc2da85f631bbe809f7cb7962608 is corrupted

$ cat packed-refs
cat: packed-refs: No such file or directory

$ git fsck --full
fatal: object 016660b7605cfc2da85f631bbe809f7cb7962608 is corrupted

After moving the file back out:

$ git fsck --full`  
error: HEAD: invalid sha1 pointer 016660b7605cfc2da85f631bbe809f7cb7962608
error: refs/heads/RPG does not point to a valid object!
dangling tree 2c1033501b82e301d47dbf53ba0a199003af25a8
dangling blob 531aca5783033131441ac7e132789cfcad82d06d
dangling blob 74a47ff40a8c5149a8701c2f4b29bba408fa36f5
dangling blob b8df4d9751c0518c3560e650b21a182ea6d7bd5e
dangling blob fc2d15aead4bd0c197604a9f9822d265bb986d8b

After unpacking the .pack file:

$ git log
fatal: bad object HEAD

$ cat packed-refs
cat: packed-refs: No such file or directory

$ git fsck --full
error: HEAD: invalid sha1 pointer 016660b7605cfc2da85f631bbe809f7cb7962608
error: refs/heads/RPG does not point to a valid object!
dangling tree 2c1033501b82e301d47dbf53ba0a199003af25a8
dangling blob 531aca5783033131441ac7e132789cfcad82d06d
dangling blob 74a47ff40a8c5149a8701c2f4b29bba408fa36f5
dangling blob b8df4d9751c0518c3560e650b21a182ea6d7bd5e
dangling blob fc2d15aead4bd0c197604a9f9822d265bb986d8b

解决方案

Okay, so. We can see from the second error message that the corrupt object which you moved was a commit. (HEAD was pointing to it!) Unfortunately, this means that it's hard to manually repair it. (By "hard" I mean likely impossible unless you can remember exactly what the commit message was and what time you made the commit.) Fortunately, this does mean that it's easy to resurrect a new commit with the same file contents - you'll just have to write a new message for it.

Before you start, have a look at the contents of .git/HEAD - if it's a branch name, remember that for later.

First, we need to figure out what the parent of this commit should've been. You can use git reflog to look at the reflog of HEAD, and find the SHA1 of where HEAD was just before you made commit 016660b. It should look something like this:

016660b HEAD@{n}: commit: <subject of commit>
1234abc HEAD@{n-1}: ...

You can copy the SHA1 of the previous position of HEAD, and check out that commit:

git checkout 1234abc

Then you can read in the tree that your corrupted commit had:

git read-tree 2c1033501b82e301d47dbf53ba0a199003af25a8

And then commit!

git commit

Now, there's some question here about what should've happened to your branches. If HEAD was pointing to a branch (say master) which in turn pointed to the corrupted commit, we definitely want to fix that up:

git branch -d master       # remove the original master branch
git checkout -b master     # recreate it here

If there are other branches which contained the corrupted commit, you'll have to do some restoration on them too - let me know if you need help with that.

这篇关于你如何在git仓库中恢复损坏的对象(对于新手)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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