Git子模块头'引用不是树'错误

本文介绍了如何解决Git子模块出现'引用不是树'错误的问题,包括从子模块内部和外部两种方式进行修复。建议先确定需要使用的子模块提交,然后在子模块中切换到该提交,更新超级项目;或者从超级项目中查找错误的提交,恢复子模块到正确状态。如果遇到问题,可能需要更新Git版本或检查二进制文件。
摘要由CSDN通过智能技术生成

本文翻译自:Git submodule head 'reference is not a tree' error

I have a project with a submodule that is pointing to an invalid commit: the submodule commit remained local and when I try to fetch it from another repo I get: 我有一个带有子模块的项目,该子模块指向无效的提交:子模块提交仍然是本地的,当我尝试从另一个repo获取它时,我得到:

$ git submodule update
fatal: reference is not a tree: 2d7cfbd09fc96c04c4c41148d44ed7778add6b43
Unable to checkout '2d7cfbd09fc96c04c4c41148d44ed7778add6b43' in submodule path 'mysubmodule'

I know what the submodule HEAD should be, is there any way I can change this locally, without pushing from the repo that does have commit 2d7cfbd09fc96c04c4c41148d44ed7778add6b43 ? 我知道子模块HEAD应该是什么,有什么办法,我可以在当地改变这一状况,在不脱离确实有承诺回购推2d7cfbd09fc96c04c4c41148d44ed7778add6b43

I'm not sure if I'm being clear... here's a similar situation I found. 我不确定我是否清楚...... 这是我发现的类似情况


#1楼

参考:https://stackoom.com/question/92qN/Git子模块头-引用不是树-错误


#2楼

Your submodule history is safely preserved in the submodule git anyway. 无论如何,您的子模块历史记录都安全地保存在子模块git中。

So, why not just delete the submodule and add it again? 那么,为什么不删除子模块并重新添加呢?

Otherwise, did you try manually editing the HEAD or the refs/master/head within the submodule .git 否则,您是否尝试在子模块.git手动编辑HEADrefs/master/head


#3楼

Assuming the submodule's repository does contain a commit you want to use (unlike the commit that is referenced from current state of the super-project), there are two ways to do it. 假设子模块的存储库确实包含您要使用的提交(与从超级项目的当前状态引用的提交不同),有两种方法可以执行此操作。

The first requires you to already know the commit from the submodule that you want to use. 第一个要求您已经知道要使用的子模块的提交。 It works from the “inside, out” by directly adjusting the submodule then updating the super-project. 它通过直接调整子模块然后更新超级项目从“内部,外部”工作。 The second works from the “outside, in” by finding the super-project's commit that modified the submodule and then reseting the super-project's index to refer to a different submodule commit. 第二个工作来自“外部,中”,通过查找超级项目的修改子模块的提交,然后重新设置超级项目的索引以引用不同的子模块提交。

Inside, Out 反了

If you already know which commit you want the submodule to use, cd to the submodule, check out the commit you want, then git add and git commit it back in the super-project. 如果你已经知道你希望子模块使用哪个提交,请cd到子模块,检查你想要的提交,然后git addgit commitgit commit回超级项目。

Example: 例:

$ git submodule update
fatal: reference is not a tree: e47c0a16d5909d8cb3db47c81896b8b885ae1556
Unable to checkout 'e47c0a16d5909d8cb3db47c81896b8b885ae1556' in submodule path 'sub'

Oops, someone made a super-project commit that refers to an unpublished commit in the submodule sub . 哎呀,有人做了一个超级项目提交,它引用了子模块sub未发布的提交。 Somehow, we already know that we want the submodule to be at commit 5d5a3ee314476701a20f2c6ec4a53f88d651df6c . 不知何故,我们已经知道我们希望子模块处于提交5d5a3ee314476701a20f2c6ec4a53f88d651df6c Go there and check it out directly. 去那里直接看看。

Checkout in the Submodule 子模块中的结帐

$ cd sub
$ git checkout 5d5a3ee314476701a20f2c6ec4a53f88d651df6c
Note: moving to '5d5a3ee314476701a20f2c6ec4a53f88d651df6c' which isn't a local branch
If you want to create a new branch from this checkout, you may do so
(now or later) by using -b with the checkout command again. Example:
  git checkout -b <new_branch_name>
HEAD is now at 5d5a3ee... quux
$ cd ..

Since we are checking out a commit, this produces a detached HEAD in the submodule. 由于我们正在检查提交,因此在子模块中生成一个分离的HEAD。 If you want to make sure that the submodule is using a branch, then use git checkout -b newbranch <commit> to create and checkout a branch at the commit or checkout the branch that you want (eg one with the desired commit at the tip). 如果你想确保子模块正在使用一个分支,那么使用git checkout -b newbranch <commit>git checkout -b newbranch <commit>时创建并签出一个分支,或者检查你想要的分支(例如,在提示处有所需提交的分支) )。

Update the Super-project 更新超级项目

A checkout in the submodule is reflected in the super-project as a change to the working tree. 子模块中的结账在超级项目中反映为对工作树的更改。 So we need to stage the change in the super-project's index and verify the results. 因此,我们需要在超级项目的索引中进行更改并验证结果。

$ git add sub

Check the Results 检查结果

$ git submodule update
$ git diff
$ git diff --cached
diff --git c/sub i/sub
index e47c0a1..5d5a3ee 160000
--- c/sub
+++ i/sub
@@ -1 +1 @@
-Subproject commit e47c0a16d5909d8cb3db47c81896b8b885ae1556
+Subproject commit 5d5a3ee314476701a20f2c6ec4a53f88d651df6c

The submodule update was silent because the submodule is already at the specified commit. 子模块更新是静默的,因为子模块已经在指定的提交中。 The first diff shows that the index and worktree are the same. 第一个差异显示索引和工作树是相同的。 The third diff shows that the only staged change is moving the sub submodule to a different commit. 第三个差异表明,唯一的分阶段变化是将sub子模块移动到另一个提交。

Commit 承诺

git commit

This commits the fixed-up submodule entry. 这将提交修复后的子模块条目。


Outside, In 外面,在

If you are not sure which commit you should use from the submodule, you can look at the history in the superproject to guide you. 如果您不确定应该从子模块中使用哪个提交,则可以查看超级项目中的历史记录以指导您。 You can also manage the reset directly from the super-project. 您还可以直接从超级项目管理重置。

$ git submodule update
fatal: reference is not a tree: e47c0a16d5909d8cb3db47c81896b8b885ae1556
Unable to checkout 'e47c0a16d5909d8cb3db47c81896b8b885ae1556' in submodule path 'sub'

This is the same situation as above. 这与上述情况相同。 But this time we will focus on fixing it from the super-project instead of dipping into the submodule. 但这一次我们将重点从超级项目中修复它,而不是浸入子模块中。

Find the Super-project's Errant Commit 找到超级项目的错误提交

$ git log --oneline -p -- sub
ce5d37c local change in sub
diff --git a/sub b/sub
index 5d5a3ee..e47c0a1 160000
--- a/sub
+++ b/sub
@@ -1 +1 @@
-Subproject commit 5d5a3ee314476701a20f2c6ec4a53f88d651df6c
+Subproject commit e47c0a16d5909d8cb3db47c81896b8b885ae1556
bca4663 added sub
diff --git a/sub b/sub
new file mode 160000
index 0000000..5d5a3ee
--- /dev/null
+++ b/sub
@@ -0,0 +1 @@
+Subproject commit 5d5a3ee314476701a20f2c6ec4a53f88d651df6c

OK, it looks like it went bad in ce5d37c , so we will restore the submodule from its parent ( ce5d37c~ ). 好吧,看起来它在ce5d37cce5d37c ,所以我们将从它的父节点恢复子模块( ce5d37c~ )。

Alternatively, you can take the submodule's commit from the patch text ( 5d5a3ee314476701a20f2c6ec4a53f88d651df6c ) and use the above “inside, out” process instead. 或者,您可以从补丁文本( 5d5a3ee314476701a20f2c6ec4a53f88d651df6c )中获取子模块的提交,并使用上面的“内部,外部”过程。

Checkout in the Super-project 超级项目结账

$ git checkout ce5d37c~ -- sub

This reset the submodule entry for sub to what it was at commit ce5d37c~ in the super-project. 这将sub的子模块条目重置为超级项目中commit ce5d37c~的内容。

Update the Submodule 更新子模块

$ git submodule update
Submodule path 'sub': checked out '5d5a3ee314476701a20f2c6ec4a53f88d651df6c'

The submodule update went OK (it indicates a detached HEAD). 子模块更新正常(它表示已分离的HEAD)。

Check the Results 检查结果

$ git diff ce5d37c~ -- sub
$ git diff
$ git diff --cached
diff --git c/sub i/sub
index e47c0a1..5d5a3ee 160000
--- c/sub
+++ i/sub
@@ -1 +1 @@
-Subproject commit e47c0a16d5909d8cb3db47c81896b8b885ae1556
+Subproject commit 5d5a3ee314476701a20f2c6ec4a53f88d651df6c

The first diff shows that sub is now the same in ce5d37c~ . 第一个差异显示sub现在在ce5d37c~是相同的。 The second diff shows that the index and worktree are the same. 第二个差异显示索引和工作树是相同的。 The third diff shows the only staged change is moving the sub submodule to a different commit. 第三个差异显示唯一的分阶段变化是将sub子模块移动到另一个提交。

Commit 承诺

git commit

This commits the fixed-up submodule entry. 这将提交修复后的子模块条目。


#4楼

Just to be sure, try updating your git binaries. 只是为了确定,尝试更新你的git二进制文件。

GitHub for Windows has the version git version 1.8.4.msysgit.0 which in my case was the problem. GitHub for Windows有版本git version 1.8.4.msysgit.0 ,在我的例子中是问题。 Updating solved it. 更新解决了它。


#5楼

Possible cause 可能的原因

This can happens when: 这可能发生在:

  1. Submodule(s) have been edited in place 子模块已经过编辑
  2. Submodule(s) committed, which updates the hash of the submodule being pointed to 提交的子模块它更新被指向的子模块的哈希值
  3. Submodule(s) not pushed . 子模块未被推动

eg something like this happened: 例如这样的事情发生了:

$ cd submodule
$ emacs my_source_file  # edit some file(s)
$ git commit -am "Making some changes but will forget to push!"

Should have submodule pushed at this point. 此时应该有子模块。

$ cd .. # back to parent repository
$ git commit -am "updates to parent repository"
$ git push origin master

As a result, the missing commits could not possibly be found by the remote user because they are still on the local disk. 因此,远程用户无法找到丢失的提交,因为它们仍在本地磁盘上。

Solution

Informa the person who modified the submodule to push, ie 将修改子模块的人告知,即

$ cd submodule
$ git push

#6楼

I got this error when I did: 我做的时候遇到了这个错误:

$ git submodule update --init --depth 1

but the commit in the parent project was pointing at an earlier commit. 但是父项目中的提交指向了先前的提交。

Deleting the submodule folder and running: 删除子模块文件夹并运行:

$ git submodule update --init

did NOT solve the problem. 没有解决问题。 I deleted the repo and tried again without the depth flag and it worked. 我删除了回购并再次尝试没有深度标志,它工作。

This error happens in Ubuntu 16.04 git 2.7.4, but not on Ubuntu 18.04 git 2.17, TODO find exact fixing commit or version. 这个错误发生在Ubuntu 16.04 git 2.7.4中,但在Ubuntu 18.04 git 2.17上没有,TODO找到了确切的修复提交或版本。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值