Git子模块头'引用不是树'错误 [英] Git submodule head 'reference is not a tree' error

查看:1914
本文介绍了Git子模块头'引用不是树'错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个子模块项目指向一个无效的提交:子模块提交仍然是本地的,当我尝试从另一个回购中获取它时:



<$
> $ git submodule update
致命:引用不是树:2d7cfbd09fc96c04c4c41148d44ed7778add6b43
无法在子模块路径'mysubmodule'中检出'2d7cfbd09fc96c04c4c41148d44ed7778add6b43'

我知道子模块HEAD应该是什么,有没有什么办法可以在本地更改它, 提交 2d7cfbd09fc96c04c4c41148d44ed7778add6b43



我不确定我是否清楚.. 。我发现了类似的情况

解决方案

假设子模块的存储库确实包含您想要使用的提交(与从cur引用的提交不同租用超级项目的状态),有两种方法可以做到这一点。第一个要求你已经知道你想要使用的子模块的提交。它通过直接调整子模块然后更新超级项目从内部,外部工作。第二种方法是通过查找超级项目的提交来修改子模块,然后重置超级项目的索引以引用不同的子模块提交,从outside,in开始工作。



< h2> Inside,Out

如果您已经知道要使用哪个子模块,请将子模块 cd ,检查你想要的提交,然后在超级项目中找到 git add git commit p>

示例:

$ p $ $ git子模块更新
致命参考不是树:e47c0a16d5909d8cb3db47c81896b8b885ae1556
无法在子模块路径'sub'

中签出'e47c0a16d5909d8cb3db47c81896b8b885ae1556' >糟糕,有人做了一个超级项目提交,它指向子模块 sub 中的一个未发布的提交。不知何故,我们已经知道我们希望子模块在提交 5d5a3ee314476701a20f2c6ec4a53f88d651df6c

在子模块中结帐



  $ cd sub 
$ git checkout 5d5a3ee314476701a20f2c6ec4a53f88d651df6c
注意:移动到'5d5a3ee314476701a20f2c6ec4a53f88d651df6c'这不是一个本地分支
如果你想从这个结账中创建一个新的分支,你可以这样做
(现在或以后)再次使用-b和checkout命令。例如:
git checkout -b< new_branch_name>
HEAD现在在5d5a3ee ... quux
$ cd ..

由于我们正在检出一个提交,因此会在子模块中生成一个分离的HEAD。如果你想确保子模块正在使用一个分支,那么使用 git checkout -b newbranch< commit> 在提交或签出时创建和签出分支你需要的分支(例如一个在提示中有所需的提交)。



更新超级项目



子模块中的结账作为对工作树的更改反映在超级项目中。因此,我们需要在超级项目的索引中进行更改并验证结果。

  $ git add sub 



检查结果



  $ git submodule update 
$ git diff
$ git diff --cached
diff --git c / sub i / sub
index e47c0a1..5d5a3ee 160000
--- c / sub
+++ i / sub
@@ -1 +1 @@
- 子项目提交e47c0a16d5909d8cb3db47c81896b8b885ae1556
+子项目提交5d5a3ee314476701a20f2c6ec4a53f88d651df6c

子模块更新是无提示的,因为子模块已经在指定的提交。第一个差异表明索引和工作树是相同的。第三个差异表明,唯一的阶段性变化是将子模块移动到另一个提交。



Commit < h3>

  git commit 

此提交固定子模块条目。






Outside,In



如果您不确定应该从子模块使用哪个提交,则可以查看超级项目中的历史记录来指导您。您可以直接从超级项目管理重置。

  $ git子模块更新
致命:引用不是一棵树:e47c0a16d5909d8cb3db47c81896b8b885ae1556
无法在子模块路径'sub'



中检出'e47c0a16d5909d8cb3db47c81896b8b885ae1556'与上述情况相同。但是这一次,我们将集中精力从超级项目中解决它,而不是陷入子模块。



找到超级项目的Errant Commit



  $ git log --oneline -p  -  sub 
ce5d37c本地更改子
diff --git a / sub b / sub
index 5d5a3ee..e47c0a1 160000
--- a / sub
+++ b / sub
@@ -1 +1 @@
- 子项目提交5d5a3ee314476701a20f2c6ec4a53f88d651df6c
+子项目提交e47c0a16d5909d8cb3db47c81896b8b885ae1556
bca4663增加了子
diff --git a / b / b
新文件模式160000
索引0000000..5d5a3ee
--- / dev / null
+++ b / sub
@@ -0,0 +1 @@
+子项目提交5d5a3ee314476701a20f2c6ec4a53f88d651df6c

好的,它在 ce5d37c 中看起来很糟糕,所以我们将从子模块中恢复它的父元素( ce5d37c〜)。



或者,你可以从补丁文本中获取子模块的提交信息( 5d5a3ee314476701a20f2c 6ec4a53f88d651df6c ),然后使用上面的inside,out过程。



在Super-project中签出



  $ git checkout ce5d37c〜 -  sub 

这会将 sub 的子模块条目重置为超级项目中提交 ce5d37c〜时的内容。

更新子模块



  $ git子模块更新
子模块路径'sub':检出'5d5a3ee314476701a20f2c6ec4a53f88d651df6c'

子模块更新成功(表示分离HEAD)。



检查结果



  $ 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 @@
- 子项目提交e47c0a16d5909d8cb3db47c81896b8b885ae1556
+子项目提交5d5a3ee314476701a20f2c6ec4a53f88d651df6c

第一个差异显示 sub 现在在<$ c $中相同C> ce5d37c〜。第二个差异表明索引和工作树是相同的。第三个差异显示了唯一的阶段性变化是将子模块移动到另一个提交。



提交



  git commit 

这提交了固定子模块条目。


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:

$ 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 ?

I'm not sure if I'm being clear... here's a similar situation I found.

解决方案

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.

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. Somehow, we already know that we want the submodule to be at commit 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. 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 (e.g. one with the desired commit at the tip).

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.

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~).

Alternatively, you can take the submodule's commit from the patch text (5d5a3ee314476701a20f2c6ec4a53f88d651df6c) and use the above "inside, out" process instead.

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.

Update the Submodule

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

The submodule update went OK (it indicates a detached 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~. 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.

Commit

git commit

This commits the fixed-up submodule entry.

这篇关于Git子模块头'引用不是树'错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆