是什么导致 git 中的子模块冲突,应该如何解决?

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

what causes submodule conflicts in git, and how should they be resolved?

gitmergegit-submodulesconflict

提问by user561638

We are using submodules and we are new to git.

我们正在使用子模块,我们是 git 的新手。

We often see merge conflicts for the submodules themselves, no files are conflicted, just the submodule. There are multiple versions listed in the output of git submodule summary. We resolve them by running git add <submodule>in the superproject.
But today we had a developer lose a commit of the submodule when she resolved the conflict in this manner.
Does running a git add choose the remote version? Shouldn't the contents of the submodule get merged? If she made changes in the submodule and committed them (which I see), then why would that commit disappear after she ran the pull and resolved the conflict?

我们经常看到子模块本身的合并冲突,没有文件冲突,只有子模块。的输出中列出了多个版本git submodule summary。我们通过git add <submodule>在超级项目中运行来解决它们。
但是今天我们有一位开发人员在以这种方式解决冲突时丢失了子模块的提交。
运行 git add 是否选择远程版本?子模块的内容不应该合并吗?如果她在子模块中进行了更改并提交了它们(我看到了),那么为什么在她运行 pull 并解决冲突后该提交会消失?

回答by Andrew Wagner

Your local submodule and the remote submodule have diverged.

您的本地子模块和远程子模块发生了分歧。

git checkout --theirs submodulename

or for your version:

或对于您的版本:

git checkout --ours submodulename

and then commit the changes with git add and commit the changes.

然后使用 git add 提交更改并提交更改。

Note: Your shell may add a trailing slash to the submodulename if you tabcomplete, since it is also a subdirectory. If so, you need to delete it or you'll get:

注意:如果你使用 tabcomplete,你的 shell 可能会在 submodulename 后面添加一个斜杠,因为它也是一个子目录。如果是这样,你需要删除它,否则你会得到:

error: pathspec 'submodulename/' did not match any file(s) known to git.

回答by charley

Both fileconflicts and submoduleconflicts occur when your current branch and the branch-you-want-to-merge-into have diverged.

这两个文件的冲突和子模块发生冲突时,您的当前分支,分支任您想对合并,变成有分歧

It merely means an ambiguous situation exists -- you could legitimately want eitherto "win" in any given case. So, while it may seem "annoying", they merely highlight your rich options to specify what you want (and you mustspecify what you want). (And, all that programmers do every day is merely to specify detail.)

这仅仅意味着一个模棱两可的情况出现-你可以合法地希望无论在任何给定的情况下,“赢”。因此,虽然它可能看起来很“烦人”,但它们只是突出显示您丰富的选项来指定您想要什么(并且您必须指定您想要什么)。(而且,程序员每天所做的只是指定细节。)

It seems like the git-add-the-submodule-on-the-superproject should have worked. However, you also had the option to git-checkout-on-the-superproject right away. This is mentioned in this link (resolving submodule conflicts), which talks about the difference between fileconflicts and summoduleconflicts, and how to resolve them:

似乎 git-add-the-submodule-on-the-superproject 应该有效。但是,您还可以立即选择 git-checkout-on-the-superproject。这个链接(解决子模块冲突)中提到了这一点,它讨论了文件冲突和summodule冲突之间的区别,以及如何解决它们:

http://pacific.mpi-cbg.de/wiki/index.php/Git_Conflicts

http://pacific.mpi-cbg.de/wiki/index.php/Git_Conflicts