165

我有一个 git 超级项目,它引用了几个子模块,我试图锁定一个工作流程,让我的项目成员在其中工作。

对于这个问题,假设我的超级项目被调用supery并且子模块被调用subby。(然后是我正在尝试做的事情的简化......我实际上并没有将分支用于版本,但我认为最容易提出问题。)

我的 master 分支将 git 项目supery的标签作为子模块引用。的分支调用并将子模块的引用更改为指向 的标签。v1.0subbysuperyone.onev1.1subby

我可以毫不费力地在这些分支中的每一个中工作,但是如果我尝试one.one使用来自分支的更改来更新分支,master我会收到一些冲突并且我不知道如何解决它们。

基本上在分支中运行一段git pull . master时间后subby,它看起来像是创建了额外的子模块。

在拉/合并之前,我git submoduleone.one分支获得了所需的响应:

$ git checkout master
$ git submodule
qw3rty...321e subby (v1.0)
$ git checkout one.one
$ git submodule
asdfgh...456d subby (v1.1)

但是在拉取之后,它会在我运行时添加额外的子模块git submodule

$ git pull . master
Auto-merged schema
CONFLICT (submodule): Merge conflict in subby - needs qu3rty...321e
Automatic merge failed; fix conflicts and then commit the results.

$ git submodule
qw3rty...321e subby (v1.0)
asdfgh...456d subby (v1.1)
zxcvbn...7890 subby (v1.1~1)

如何删除/忽略不需要的子模块引用并提交我的冲突和更改?或者是否有一个参数可以与我的原始参数一起使用,git pull它会忽略我的子模块?

4

9 回答 9

115

Well, its not technically managing conflicts with submodules (ie: keep this but not that), but I found a way to continue working...and all I had to do was pay attention to my git status output and reset the submodules:

git reset HEAD subby
git commit

That would reset the submodule to the pre-pull commit. Which in this case is exactly what I wanted. And in other cases where I need the changes applied to the submodule, I'll handle those with the standard submodule workflows (checkout master, pull down the desired tag, etc).

于 2009-05-07T15:33:52.750 回答
76

I struggled a bit with the answers on this question and didn't have much luck with the answers in a similar SO post either. So this is what worked for me - bearing in mind that in my case, the submodule was maintained by a different team, so the conflict came from different submodule versions in master and my local branch of the project I was working on:

  1. Run git status - make a note of the submodule folder with conflicts
  2. Reset the submodule to the version that was last committed in the current branch:

    git reset HEAD path/to/submodule

  3. At this point, you have a conflict-free version of your submodule which you can now update to the latest version in the submodule's repository:

    cd path/to/submodule
    git submodule foreach git pull origin SUBMODULE-BRANCH-NAME
  4. And now you can commit that and get back to work.

于 2015-09-15T08:59:07.337 回答
31

I have not seen that exact error before. But I have a guess about the trouble you are encountering. It looks like because the master and one.one branches of supery contain different refs for the subby submodule, when you merge changes from master git does not know which ref - v1.0 or v1.1 - should be kept and tracked by the one.one branch of supery.

If that is the case, then you need to select the ref that you want and commit that change to resolve the conflict. Which is exactly what you are doing with the reset command.

This is a tricky aspect of tracking different versions of a submodule in different branches of your project. But the submodule ref is just like any other component of your project. If the two different branches continue to track the same respective submodule refs after successive merges, then git should be able to work out the pattern without raising merge conflicts in future merges. On the other hand you if switch submodule refs frequently you may have to put up with a lot of conflict resolving.

于 2009-05-07T17:33:16.037 回答
24

I had this problem with git rebase -i origin/master to a branch. I wanted to take master's version of the submodule ref, so I simply did:

git reset master path/to/submodule

and then

git rebase --continue

That solved the problem for me.

于 2017-05-01T13:35:41.617 回答
18

First, find the hash you want to your submodule to reference. then run

~/supery/subby $ git co hashpointerhere
~/supery/subby $ cd ../
~/supery $ git add subby
~/supery $ git commit -m 'updated subby reference'

that has worked for me to get my submodule to the correct hash reference and continue on with my work without getting any further conflicts.

于 2013-01-09T02:01:29.713 回答
6

Got help from this discussion. In my case the

git reset HEAD subby
git commit

worked for me :)

于 2017-08-29T09:20:13.833 回答
3

Well In my parent directory I see:

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Unmerged paths:
(use "git reset HEAD <file>..." to unstage)
(use "git add <file>..." to mark resolution)

So I just did this

git reset HEAD linux
于 2018-01-16T08:25:59.653 回答
2

All the above didn't work for me... I had the submodule under "unmerged path" and I was stuck...

Eventually after many many attempts what worked was: deleting manually the merge conflicts file from the sub-module than in main repo:

git restore --staged submodule_path

(this moved it to "Changes not staged for commit:")

 git clean -df

then

git submodule update --init

于 2021-10-11T18:10:40.647 回答
1

If you want to use the upstream version:

rm -rf <submodule dir>
git submodule init
git submodule update
于 2020-09-16T15:18:20.040 回答