【git】failed to merge submodule ...
阿新 • • 發佈:2022-12-05
資料來源
(1) https://git-scm.com/book/en/v2/Git-Tools-Submodules
1.原文
Merging Submodule Changes If you change a submodule reference at the same time as someone else, you may run into some problems.
That is, if the submodule histories have diverged and are committed to diverging branches in a superproject,
it may take a bit of work foryou to fix. If one of the commits is a direct ancestor of the other (a fast-forward merge), then Git will simply choose the latter for the merge,
so that works fine. Git will not attempt even a trivial merge for you, however. If the submodule commits diverge and need to be merged,
you will get something that looks like this: $ git pull remote: Counting objects: 2, done. remote: Compressing objects: 100% (1/1), done. remote: Total 2 (delta 1), reused 2 (delta 1) Unpacking objects: 100% (2/2), done. From https://github.com/chaconinc/MainProject 9a377d1..eb974f8 master -> origin/master Fetching submodule DbConnector warning: Failed to merge submodule DbConnector (merge following commits not found)Auto-merging DbConnector CONFLICT (submodule): Merge conflict in DbConnector Automatic merge failed; fix conflicts and then commit the result. So basically what has happened here is that Git has figured out that the two branches record points in the submodule’s history that
are divergent and need to be merged. It explains it as “merge following commits not found”, which is confusing but we’ll explain why
that is in a bit. To solve the problem, you need to figure out what state the submodule should be in. Strangely, Git doesn’t really give you much
information to help out here, not even the SHA-1s of the commits of both sides of the history. Fortunately, it’s simple to figure out.
If you run git diff you can get the SHA-1s of the commits recorded in both branches you were trying to merge. $ git diff diff --cc DbConnector index eb41d76,c771610..0000000 --- a/DbConnector +++ b/DbConnector So, in this case, eb41d76 is the commit in our submodule that we had and c771610 is the commit that upstream had.
If we go into our submodule directory, it should already be on eb41d76 as the merge would not have touched it.
If for whatever reason it’s not, you can simply create and checkout a branch pointing to it. What is important is the SHA-1 of the commit from the other side. This is what you’ll have to merge in and resolve.
You can either just try the merge with the SHA-1 directly, or you can create a branch for it and then try to merge that in.
We would suggest the latter, even if only to make a nicer merge commit message. So, we will go into our submodule directory, create a branch named “try-merge” based on that second SHA-1 from git diff,
and manually merge. $ cd DbConnector $ git rev-parse HEAD eb41d764bccf88be77aced643c13a7fa86714135 $ git branch try-merge c771610 $ git merge try-merge Auto-merging src/main.c CONFLICT (content): Merge conflict in src/main.c Recorded preimage for 'src/main.c' Automatic merge failed; fix conflicts and then commit the result. We got an actual merge conflict here, so if we resolve that and commit it, then we can simply update the main project with the result. $ vim src/main.c (1) $ git add src/main.c $ git commit -am 'merged our changes' Recorded resolution for 'src/main.c'. [master 9fd905e] merged our changes $ cd .. (2) $ git diff (3) diff --cc DbConnector index eb41d76,c771610..0000000 --- a/DbConnector +++ b/DbConnector @@@ -1,1 -1,1 +1,1 @@@ - Subproject commit eb41d764bccf88be77aced643c13a7fa86714135 -Subproject commit c77161012afbbe1f58b5053316ead08f4b7e6d1d ++Subproject commit 9fd905e5d7f45a0d4cbc43d1ee550f16a30e825a $ git add DbConnector (4) $ git commit -m "Merge Tom's Changes" (5) [master 10d2c60] Merge Tom's Changes First we resolve the conflict. Then we go back to the main project directory. We can check the SHA-1s again. Resolve the conflicted submodule entry. Commit our merge. It can be a bit confusing, but it’s really not very hard.