如何修复缺少对象的 git 存储库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8347796/
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-19 06:13:01  来源:igfitidea点击:

How can I repair a git repository with a missing object?

git

提问by Schwern

My development repository at some point lost an object.

我的开发存储库在某个时候丢失了一个对象。

$ git fsck
fatal: failed to read object 2dddc84156fa30e4614a7ea5a1895885011b8db8: Invalid argument
$ git cat-file -t 2dddc84156fa30e4614a7ea5a1895885011b8db8
error: unable to find 2dddc84156fa30e4614a7ea5a1895885011b8db8
fatal: git cat-file 2dddc84156fa30e4614a7ea5a1895885011b8db8: bad file

A freshly cloned repository has that object.

新克隆的存储库具有该对象。

$ git cat-file -t 2dddc84156fa30e4614a7ea5a1895885011b8db8
tree

I would like to keep my development repository, it has unpushed branches and stashes.

我想保留我的开发存储库,它有未推送的分支和存储。

How can I patch that object into my broken repository?

如何将该对象修补到损坏的存储库中?

采纳答案by klaustopher

What you can try, is to get the file .git/objects/2d/ddc84156fa30e4614a7ea5a1895885011b8db8from the freshly cloned repository and copy it to your development repo ...

您可以尝试的是.git/objects/2d/ddc84156fa30e4614a7ea5a1895885011b8db8从新克隆的存储库中获取文件并将其复制到您的开发存储库...

回答by Carl

It pretty much boils down to this: do you have a backup of that file (.git/objects/2d/ddc84156fa30e4614a7ea5a1895885011b8db8) anywhere? If you can find it, copy it back to your git repository and everything will be good again. In other words, do you know if the repository has been cloned? If it has, you will find that file in the clone's git folder and copy it to yours. If you have not cloned the repository, have you zipped it up somewhere? If you're on a mac, have you checked Time machine? If you're not on a mac, are you using something to backup your code?

它几乎可以归结为:您是否在任何地方备份了该文件(.git/objects/2d/ddc84156fa30e4614a7ea5a1895885011b8db8)?如果你能找到它,把它复制回你的 git 存储库,一切都会好起来的。换句话说,您是否知道存储库是否已被克隆?如果有,您将在克隆的 git 文件夹中找到该文件并将其复制到您的文件夹中。如果您还没有克隆存储库,您是否将其压缩到某个地方?如果您使用的是 mac,您是否检查过 Time machine?如果您使用的不是 Mac,您是否正在使用某种东西来备份您的代码?

It basically boils down to this: You need a backup of that file. Git is designed so that when this happens, you just use the backup (which is a clone of the repository) to restore the file. That said, the file name is based on an SHA1 hash, so if you can find that filename in anyone else's repository, chances are pretty good that you can use it in your own :)

它基本上归结为:您需要备份该文件。Git 被设计为在发生这种情况时,您只需使用备份(它是存储库的克隆)来恢复文件。也就是说,文件名基于 SHA1 哈希,因此如果您可以在其他人的存储库中找到该文件名,那么您很有可能可以在自己的存储库中使用它 :)

It is usually good practice to always clone your repositories to another location, for safe keeping in case something like this happens. This is also why services like github and bitbucket are so popular.

始终将您的存储库克隆到另一个位置通常是一种很好的做法,以确保安全,以防发生这种情况。这也是 github 和 bitbucket 等服务如此受欢迎的原因。

You also probably want to look at this question: How to recover Git objects damaged by hard disk failure?

您可能还想看看这个问题:如何恢复因硬盘故障而损坏的 Git 对象?

回答by campkeith

I recently had a bunch of missing objects in a local repository and an uncorrupted reference repository to restore it from. Here's the magic I came up with that worked for me:

我最近在本地存储库中有一堆丢失的对象和一个未损坏的参考存储库来恢复它。这是我想出的对我有用的魔法:

cd <path-to-local-repo>
git unpack-objects < <reference-repo-path>/objects/pack/pack-<hash>.pack

It turns out that all of the objects I needed were in a pack file in the reference repository instead of in separate "loose" object files. This simple incantation unpacks that pack file into the local database, filling in the holes. After doing

事实证明,我需要的所有对象都在参考存储库中的一个包文件中,而不是在单独的“松散”对象文件中。这个简单的咒语将打包文件解包到本地数据库中,填补漏洞。做完之后

git gc

to cleanup any dangling commits (i.e. due to rebasing, amend commits, etc.) my git fsckis now clean.

清理任何悬空提交(即由于变基、修改提交等)我的git fsck现在是干净的。