Git: Merge and Submodules

Suppose I have two branches in RepoX called BranchA and BranchB. RepoX also has a submodule called SubmoduleY.

BranchA has a SubmoduleY revision of 'abc', BranchB has a SubmoduleY revision of 'def'.

Say I want to merge BranchA into BranchB, but I want to leave SubmoduleY BranchB, pointing to its original version of 'def'. I see a couple of ways to do this:

Method 1:

  • Place an order on BranchB.
  • Move SubmoduleY to the 'abc' revision to make the actual merge painless (we don't want to do the merge at the submodule level right now).
  • Commit a new revision for SubmoduleY (we cannot float it for merging).
  • Merge BranchA into BranchB. Elimination of any conflicts.
  • Move SubmoduleY back to the 'def' version.
  • Complete the new revision for SubmoduleY.
  • Push changes to the main repo.

Method 2:

Same as method 1, but instead of doing step 6, reinstall and get rid of fixing the additional submodule from step 3.

Both seem to have annoying flaws:

Method 1 adds two additional entries to the story.

Method 2 forgets about any changes associated with changes to the submodule as these commits are deleted. Thus, any mergers will later deal with some problems again.

Is there a better way?

+8
git merge git-submodules
source share
1 answer

You can change the variant of your method 1, but make a commit that introduces the change to the submodule version (in step 6) using --amend so that it changes the state of the submodule in the merge --amend . In other words, it will be:

 $ git checkout b $ git merge a Merge made by recursive. example.txt | 1 + sY | 2 +- 2 files changed, 2 insertions(+), 1 deletions(-) create mode 100644 example.txt $ cd sY $ git checkout def [... you get the "detached HEAD" warning ...] $ cd .. $ git add sY $ git commit --amend 

Please note that I did not try to avoid the submodules in different versions before the merge, as you suggested in your question. If there is a conflict, you can simply add a submodule to def to resolve it. If the conflict does not exist, the steps mentioned above should work fine.

+7
source share

Source: https://habr.com/ru/post/649915/


All Articles