19

我正在尝试使用 git 的子树合并策略,其中我要合并的子目录嵌套得相当深——目前有四层深。

我按照此处的说明将模块存储库添加为远程,运行 git read-tree 以将远程代码放入本地存储库的子目录中,然后提交这些更改。

当我尝试将远程更改并合并到我的主项目的主分支时,我的问题就出现了。上面页面的第 5 步建议使用 -s 子树开关进行 git pull。当我的子目录为一层、两层或三层而不是四层时,这对我来说是正确的。

这是合并到 2 层深的子目录的结果。您可以看到sites/all/ 中的README 文件已正确更新。在我的远程仓库中,自述文件位于根目录中。

$ git pull -s subtree REMOTE_REPO master
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 2), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From /path/to/my/REMOTE_REPO
 * branch            master     -> FETCH_HEAD
Merge made by subtree.
 sites/all/README |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

这里的子目录有 3 层深:sites/all/modules/。这也可以正常工作,拉取更改并更新文件。

$ git pull -s subtree REMOTE_REPO master
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 2), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From /path/to/my/REMOTE_REPO
 * branch            master     -> FETCH_HEAD
Merge made by subtree.
 sites/all/modules/README |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

但现在我的代码位于 4 层深的子目录中:sites/all/modules/my_module/。Git 似乎从 REMOTE_REPO 中提取了更改,但它不会更新文件,而是告诉我它已经是最新的。

$ git pull -s subtree REMOTE_REPO master
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 2), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From /path/to/my/REMOTE_REPO
 * branch            master     -> FETCH_HEAD
Already up-to-date!
Merge made by subtree.

如果我立即再次运行它,它不会拉取更改或更新文件。

$ git pull -s subtree REMOTE_REPO master
From /path/to/my/REMOTE_REPO
 * branch            master     -> FETCH_HEAD
Already up-to-date.

此时查看 git 日志将显示来自远程 repo 和合并的更改,但我结帐中的文件尚未更新。

这是一个错误,还是我做错了什么?

更新: Chris Johnsen 提供了以下选项,但会引发错误:

$ git pull -X subtree=sites/all/modules/my_module/ REMOTE_REPO master
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 2), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From /Users/jeff/work/checkouts/compass_suite
 * branch            master     -> FETCH_HEAD
fatal: entry  not found in tree 173d4f16af4d2d61ae5c4b3446c392e8b49cc57d
4

2 回答 2

54

Clarifying for future readers, the solution is in the comments of Chris Johnsen's response. If you're seeing the "fatal: entry not found in tree" error, get rid of the trailing slash at the end of your subtree prefix.

For example, if you are trying to pull a GitHub Pages subtree with a command like

git subtree --prefix gh-pages/ pull origin gh-pages

and there are conflicts you will get an error like

 * branch            gh-pages   -> FETCH_HEAD
fatal: entry  not found in tree 6e69aa201cad3e5888f1d12af0d910f8a10a8ec3

Just remove the trailing slash from the gh-pages directory

git subtree --prefix gh-pages pull origin gh-pages

This will work, and will try to merge. The worst scenario you can get is when the auto merge fails and you get an error like

Automatic merge failed; fix conflicts and then commit the result.

but you just have to resolve the conflicts manually and you are done.

于 2012-11-07T01:01:45.140 回答
10
于 2011-05-06T06:44:56.470 回答