恢复损坏的 git 存储库

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

Recovering broken git repository

git

提问by Eyoka

My working git repository is broken, it lose track to all the files in it, i.e.

我的工作 git 存储库坏了,它无法跟踪其中的所有文件,即

$ git log
fatal: bad default revision 'HEAD'  
$ git status  
... told me that all the files are new

However the .git directory does contain my objects.

但是 .git 目录确实包含我的对象。

$ du -sh .git
34M    .git
$ git count-objects
4151 objects, 32692 kilobytes
$ git --version
git version 1.6.0.4

The last thing I remember doing before it went wrong was creating (clone --mirror) a backup repository at an NFS-mounted server. However the backup repository cloned is broken the same way.

我记得在出错之前做的最后一件事是在安装了 NFS 的服务器上创建 (clone --mirror) 备份存储库。然而,克隆的备份存储库以同样的方式被破坏。

How can I restore my repository?

如何恢复我的存储库?

采纳答案by Cascabel

There must've been something besides the clone, but I know how hard it is to remember those things.

除了分身之外肯定还有别的东西,但我知道记住这些东西是多么困难。

The first thing you want to do is look in .git/refs and see if there's anything valid in there (I'm not too optimistic since you say that there don't appear to be any branches, but it's worth a shot). If any valid refs exist, you may be able to get some information from git-reflog.

您要做的第一件事是查看 .git/refs 并查看其中是否有任何有效内容(我不太乐观,因为您说似乎没有任何分支,但值得一试)。如果存在任何有效的引用,您可能能够从git-reflog.

Next, I would start having a look at git-fsck. Its main purpose is to verify connectivity and validity of objects in the database. Depending on what exactly has happened to your repo, you may need --unreachableor --lost-found. Hopefully the objects are intact, so all you need to do is find some dangling commit hashes to check out and recreate branches at.

接下来,我将开始查看git-fsck. 它的主要目的是验证数据库中对象的连通性和有效性。根据您的回购究竟发生了什么,您可能需要--unreachable--lost-found。希望对象是完整的,所以你需要做的就是找到一些悬空的提交哈希来检查并重新创建分支。

回答by Jb.

Try to check if each of your files in .git/ are owned by current user.

尝试检查 .git/ 中的每个文件是否归当前用户所有。

I had the same problem, when realizing I made some commits with root user, and that created objects (under .git/objects) where belongig to root, trigering errors when running git as regular user.

我遇到了同样的问题,当我意识到我使用 root 用户进行了一些提交,并且创建了属于 root 的对象(在 .git/objects 下)时,在以普通用户身份运行 git 时会触发错误。

This command solved the problem:

这个命令解决了这个问题:

sudo chown jb:jb .git/ -R *

回答by Jens

I got this problem just now after my GitHub application (PC) crashed. My branch disappeared when using git branchand it kept promting me to do my initial commit. I solved it by locating my branch in .git/refs/heads/and renaming it from mybranch.lockto just mybranch(remove the lock).

在我的 GitHub 应用程序 (PC) 崩溃后,我刚刚遇到了这个问题。我的分支在使用时消失了git branch,它一直提示我做我的初始提交。我通过找到我的分支.git/refs/heads/并将其重命名mybranch.lock为 just mybranch(移除锁)来解决它。

回答by Asrail

You can examine manually, but that would require some knowledge on the format of the repository.

您可以手动检查,但这需要一些有关存储库格式的知识。

Without looking at the repository is hard to tell what is happening, but probably some file was corrupted.

不查看存储库很难判断发生了什么,但可能某些文件已损坏。

Run git fsck and it will tell whether your repository is still valid.

运行 git fsck 它将判断您的存储库是否仍然有效。

Post the result of the git fsck run and that should help us to help you.

发布 git fsck 运行的结果,这应该有助于我们为您提供帮助。

回答by Noob-o-tron 5000

I had this issue after a developer did a $ git initinside the bare master of a centralised repo.

在开发人员$ git init在集中式存储库的裸机内部做了一个内部之后,我遇到了这个问题。

If you're working with a repository with no working directory, check for a .gitfolder; deleting this should rectify the problem.

如果您正在使用没有工作目录的存储库,请检查.git文件夹;删除它应该可以解决问题。