Git引用主分支现在为空,如何恢复?

3

我不太确定发生了什么事情,但由于某种原因,我的 git 存储库的主引用文件现在为空。我们将存储库托管在 Dropbox 上,所以可能与此有关... 但现在我无法从中拉取。它显示如下:

Your configuration specifies to merge with the ref 'master' from the remote, but no such ref was fetched.

Dropbox会保存文件的版本,因此如果我回到“主”版本的上一个版本,它会显示:
fatal: object 2d154f82e59a4156a5d3ea04a0617c243ae6dc3a is corrupted
fatal: The remote end hung up unexpectedly

我该如何从这种情况中恢复?


前一个远程主分支版本所引用的提交在你的本地仓库(或其他任何人的仓库)中可用吗? - Cascabel
当我使用当前版本替换本地存储库上的版本时,会出现上述损坏错误。 - robev
所以...你的本地仓库损坏了吗?(运行 git fsck)你的问题实际上暗示这些问题是与远程仓库有关的。 - Cascabel
他们是。本地仓库不在。 - robev
1个回答

7

叹气。

我喜欢Dropbox,但我不会建议将其用作“托管”Git存储库。它的同步对于单个文件文档已经足够好了,但它远远不能提供分布式/远程文件系统语义,这是安全托管Git存储库所需的。

希望你只是在Dropbox上“托管”一个裸的Git存储库。如果是这样,你应该能够从非裸(工作)存储库中拼凑出提交和引用。手动恢复现有存储库也是可能的,但你很可能最终需要从其他存储库复制对象,因此最好按照以下“高级”方式进行操作(而不是进行“低级”操作并处理从其他存储库复制对象(或复制包文件并解压其中的部分))。

首先,从一个工作目录中进行独立克隆。

git clone file://path/to/myrepo /path/to/myrepo-recovery-work

从其他工作仓库中导入引用和对象。

# If you have network access to the other repositories:
cd /path/to/myrepo-recovery-work
git remote add other1 user@machine:path/to/other/working-repo-1
git remote add other2 ssh://user@machine/path/to/other/working-repo-2
# etc.
git fetch --all


# If you do not have network access:
cd /path/to/other/working-repo-1 &&  # on machine with working-repo-1
  git bundle create /path/to/other1.gitbundle --all HEAD
cd /path/to/other/working-repo-2 &&  # on machine with working-repo-2
  git bundle create /path/to/other2.gitbundle --all HEAD
# etc.
# Transfer the bundle files to the machine that has "myrepo-recovery-work"
# (Dropbox is OK to copy the bundles, they are just single files),
# then on that machine:
cd /path/to/myrepo-recovery-work
git remote add other1 /path/to/transferred/other1.gitbundle
git remote add other2 /path/to/transferred/other2.gitbundle
# etc.
git fetch --all

然后,浏览您的新远程仓库中的所有分支,并确定哪些分支应指向哪些提交以重建您的中央仓库。

git log --oneline --graph --decorate --abbrev-commit --all
# In addition to looking at the history graph, you might need to examine
# the content of certain commits. Just check them out as a detached HEADs.
git branch recovered/maintenance decafbad
git branch recovered/master cafebabe
git branch recovered/development deadbeef
git branch recovered/bug-1234 8badf00d

创建并推送您恢复的分支(和标签)到一个新的裸仓库。
git init --bare /path/to/new-central-bare-repo.git # or real Git hosting service!
git remote add new /path/to/new-central-bare-repo.git
git push --tags new 'refs/heads/recovered/*:refs/heads/*'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接