如何在 git 存储库中恢复损坏的对象(对于新手)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3613682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 08:59:56  来源:igfitidea点击:

How do you restore a corrupted object in a git repository (for newbies)?

gitrecovery

提问by Humble Penguin

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.

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

I researched this problem online and found the articleby 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.

我在网上研究了这个问题并找到了Linus Torvalds的文章,但在他找到断开的链接 ID 时迷路了:我的文件 ID、树或 blob 都与错误消息抛出的罪魁祸首 ID 不匹配。

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

然后我返回到关于恢复“因硬盘故障损坏的 git 对象”的文章,并且(在将罪魁祸首对象移开之后)一直在努力,直到

$ cat packed-refs

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

此时我的电脑说:cat: packed-refs: No such file or directory我跳过了那一步并做了

$ 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 toodrastic or I might force something I can't unforce later.

并获得了适当的输出,但后来我应该将罪魁祸首(或我所说的罪魁祸首,错误抛出的 sha1 ID)从备份存储库复制回主存储库,然后从据我所知,备份存储库到主存储库中;我不想做任何激烈的事情,否则我可能会强迫一些我以后无法取消的事情。

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?

所以我的问题是(是),我应该做一个备份(哦,新手警报),还是当我解压 .pack 文件时发生了什么?我复制回来的“罪魁祸首”实际上是一个干净的文件,即没有损坏?

(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 REALLYnew at this.)

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

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:

解压 .pack 文件后:

$ 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

采纳答案by Cascabel

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.

可以,然后呢。我们可以从第二条错误消息中看到您移动的损坏对象是一个提交。(HEAD 指向它!)不幸的是,这意味着很难手动修复它。(“硬”我的意思可能是不可能的,除非你能准确记住提交消息是什么以及你提交的时间。)幸运的是,这确实意味着很容易用相同的文件内容重新启动一个新的提交——你会只需要为它写一条新消息。

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

在开始之前,先看看内容.git/HEAD- 如果是分支名称,请记住以备后用。

First, we need to figure out what the parent of this commit should've been. You can use git reflogto 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:

首先,我们需要弄清楚这次提交的父级应该是什么。您可以使用git reflog查看 HEAD 的 reflog,并在提交 016660b 之前找到 HEAD 所在位置的 SHA1。它应该是这样的:

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:

您可以复制 HEAD 前一个位置的 SHA1,并检查该提交:

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:

现在,这里有一些关于你的分支应该发生什么的问题。如果 HEAD 指向一个分支(比如 master),而该分支又指向损坏的提交,我们肯定要修复它:

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.

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

回答by underscore_d

FWIW, here's an answer that is more practical than a lot of the hopelessly optimistic ones on other questions about corrupted gitrepos - most of which make the baseless assumption that the poor OP can 'just re-clone from the remote origin!' Hmm. But.Wait a second. What if I am the origin?

FWIW,这里的答案比许多关于损坏的存储git库的其他问题的绝望乐观的答案更实用- 其中大多数都做出了毫无根据的假设,即可怜的 OP 可以“只是从远程源重新克隆!” 唔。但。等一等。如果我是原点呢?

The horror

惊恐的事件

The story begins when an attempt to run a simple git gc --aggressiverevealed that, unbeknownst to me, at some point my - completely local - gitrepo had somehow gotten completely hosed: It was unable to log anything beyond a few months ago, thereby losing the majority of its history, and screamed gutturally whenever asked to git fsck --full | grep -v dangling. Several objects were identified as lost by git fsck.

故事开始时,尝试运行一个简单的git gc --aggressive程序,我不知道,在某些时候,我的 - 完全本地的 - 存储git库不知何故完全被冲洗了:它无法记录几个月前的任何内容,从而失去了大部分历史,并在被要求时喉咙里尖叫git fsck --full | grep -v dangling。几件物品被 确定为丢失git fsck

git-repair: minimising terrifying manual labour since 2014

git-repair:自 2014 年以来最大限度地减少可怕的体力劳动

After panicking and finding lots of questions with over-voted poor answers just saying 'just re-clone from the remote origin!' - which, let me belabour the point, I don't have because I am the origin- I found git-repair, did a simple sudo aptitude install git-repair, and let it do all the tedious automatable fixes that would've probably taken me hours (please: run on a copy of your corrupted repo [duh], without --force!)

在惊慌失措并发现许多问题的答案被过度投票后,只是说“只需从远程源重新克隆!” - 让我强调一下,我没有,因为我是原点- 我发现git-repair,做了一个简单的sudo aptitude install git-repair,并让它完成所有可能需要我花费数小时的乏味的自动化修复(请:运行损坏的 repo [duh] 的副本,没有--force!)

That helped reduce the amount of horror reported by git fsck --full | grep -v dangling. But things beyond the middle of August were still unreachable.

这有助于减少git fsck --full | grep -v dangling. 但八月中旬之后的事情仍然无法实现。

In particular, this all seemed to centre around one commit that couldn't be retrieved. How could I recover it? Searching Stack Overflow wasn't much help!

特别是,这一切似乎都围绕着一个无法检索的提交。我怎样才能恢复它?搜索 Stack Overflow 并没有多大帮助!

You have a backup, right?

你有备份,对吧?

Here's where I was lucky enough to have a backupfrom the end of November. You take backups, right? In my case, it was a manual zip of the repo (my daily backup routine is some horrible incremental tarthing that I've never actually tested... a-cough)... but phew, it was good enough. It hadn't suffered whatever indignity had afflicted my live repo.

这是我有幸从 11 月底开始备份的地方。你备份,对吧?就我而言,它是 repo 的手动 zip(我的日常备份例程是一些可怕的增量tar事情,我从未实际测试过……咳咳)……但是,呸,这已经足够了。它没有遭受任何折磨我的实时回购的侮辱。

But the missing object didn't seem to be simply stored in this backup's .git/objects/XY/RESTOFHASHBLAHBLAHBLAH. That's probably because it was a commit, not a file. I don't know! gitis wizardry to me, forever beyond my capacity to comprehend. I just needed a fix, fast. Isn't that why we're all here?

但是丢失的对象似乎并没有简单地存储在这个备份的.git/objects/XY/RESTOFHASHBLAHBLAHBLAH. 那可能是因为它是一个提交,而不是一个文件。我不知道!git对我来说是魔法,永远超出我的理解能力。我只是需要一个修复,快。这不是我们都在这里的原因吗?

Recover objects from a backup (which you have, right?)

从备份中恢复对象(您拥有,对吗?)

With backup now in hand, I had a hilariously dumb idea, said 'there's no way this can work!', and immediately found that simply naively running cp -fr /path/to/repo_backup/.git/objects/* /path/to/repo_git-repaired/.git/objectsto file-merge the.git/objectsdirectory of the backup into its counterpart in my corrupted,-somewhat-repaired repo... worked to recover all history- right back to good ol' initial commit lol. Proving the fix: git fsck --fullwas now happy too (notwithstanding all the dangling bits).

通过备份现在在手,我有一个欢快的愚蠢的想法,说:“没有办法,这可以工作!”,并立即发现根本天真地运行cp -fr /path/to/repo_backup/.git/objects/* /path/to/repo_git-repaired/.git/objects该文件合并.git/objects备份的目录复制到其对应在我的损坏,-somewhat-修复回购...努力恢复所有历史- 回到良好的 ol' initial commit lol。证明修复:git fsck --full现在也很高兴(尽管有所有悬而未决的部分)。

I then took backups of the live/corrupted, partly repaired, and seemingly recovered repos onto a separate drive, in case I need any of them again.

然后我将实时/损坏的、部分修复的、看似恢复的存储库备份到一个单独的驱动器上,以防我再次需要它们中的任何一个。

You should double-check that methodology and command before you run it, or perhaps find a much better way that I'm too lazy to think of. I don't know. But for me, it saved my repo. And on the subject of second-guessing what I say...

您应该在运行之前仔细检查该方法和命令,或者也许找到我懒得想的更好的方法。我不知道。但对我来说,它拯救了我的回购。关于第二次猜测我说的话......

Infinite disclaimers in infinite combinations

无限组合中的无限免责声明

Now, obviously all of this comes with caveats: you musttry everything on a copy of your corrupted repo, read all the documentation, consider being a bit more careful than I was (that forced cpcommand), and not hold me responsible or liable for absolutely anything that goes wrong*... but it gives you something better to try than 'just re-clone from the remote origin!', right?

现在,显然所有这些都带有警告:您必须在损坏的存储库副本上尝试所有内容,阅读所有文档,考虑比我更小心(强制cp命令),并且不让我负责或承担任何责任绝对没有任何问题*...但它为您提供了比“仅从远程源重新克隆!”更好的尝试,对吗?

*If, however, everything goes right, a large donation may be in order. ;)

*但是,如果一切顺利的权利,大捐赠可能是为了。;)

If anyone needs me, I'll be glaring suspiciously in the general direction of my disk drive, hopefully coming up with a backup routine that won't take double-digit hours to restore from (if at all), and maybe even getting some sleep.

如果有人需要我,我会怀疑地盯着我的磁盘驱动器的大方向,希望能提出一个备份程序,它不会花费两位数的时间来恢复(如果有的话),甚至可能得到一些睡觉。

回答by Ruhan

I had the same problem. However, my problem was resolved with changing permissions of the .git/objects folders and subfolders (recursively) on server. Something like:

我有同样的问题。但是,通过更改服务器上 .git/objects 文件夹和子文件夹(递归)的权限解决了我的问题。就像是:

chmod -R 770 .git/objects

I think that isn't your problem, but in my case it was resolved.

我认为这不是您的问题,但就我而言,它已解决。