Git merge between branches with submodules

Asked

Viewed 1,259 times

4

I have a following mobile project. A repository where code is shared between several projects. I’ll call this repository.

In the projects were created branchs of this base type because it was necessary to add a different sub-module for each project.

Ex. Project 1. Created the project branch 1 in the base repository and added the Sub1 submodule. Project 2. Created the project branch 2 in the base repository and added the sub2 submodule.

So... the difference between master and base branches is that branches have submodule. However, throughout the development, the project 1 branch has been modified and I need to send these modifications to the master and project branch.

Being in the master branch and running a git merge project1, it turns out that the project submodule 1 is included in the master, and this cannot happen, because the master is only to serve as the basis for the projects.

How to solve this?

4 answers

3

The interesting thing in this case is whenever you make a change, create a branch from master because then you can merge with any other branch without taking the submodules. Staying:

master
|----- projeto1
|-------- projeto2
|- novo branch

After creating the update, merge this new branch into project 1, project 2 and master. After merging into master you can even delete this new branch.

To solve your spot problem, I believe the best solution is to use the Cherry-pick command. There you apply commits (regardless of which branch you are on) to the current branch.

It would be something like:

$ git cherry-pick <hash do 1º commit a enviar>..<hash do ultimo commit a enviar>

The following is a link to the documentation on how to use it:

http://git-scm.com/docs/git-cherry-pick

  • I liked your solution Lucas. But I’m not going to do it. If it were, I would do it the way you suggested, but my colleague who is on this project is super lazy and will not want to create new branches because it makes testing difficult. Thank you

2


First, merge with the parameter --no-commit:

git merge --no-ff --no-commit projeto1

At that point, the merge will have been done, but the commit has not yet been performed. Take the opportunity to remove the submodule:

git rm caminho/do/submodulo

It may be necessary to use the git submodule deinit first -- the documentation seems to indicate that it is not necessary, but I have never removed a module.

Once done, continue with the merge using git commit.

Done. The merge will include the removal of the submodule.

From then on, you will be able to do merges normally as long as no changes have been made to the sub-module. Changes to the submodule will likely result in merge Conflicts, which will allow you to intercede when merging. If this is not the case, you will have to repeat the above procedure whenever there are changes in the sub-module.

  • Daniel. But in case I have to send this update to the other branches? Example: modifications to the project branch 1, merge in master, remove the submodule and then merge into project2. Will it work perfectly?

  • @Lucianolima If you send things from master to branches, then you will have the reverse problem -- the merge will remove the submodule. The solution in this case is to do the same thing both ways: first merge into the master by removing the submodule, and then you use the same procedure to merge from the master to the branch by adding the submodule back. After that, merges in both directions will preserve the status of the submodule in that branch.

0

Perhaps a better way to organize is for you to turn what you call a base into a submodule and the project branches into new repositories that used the base submodule. In this way, common code fixes would be more easily shared between projects.

If that’s not feasible, I suggest rebasing your commits into branches to separate commits that make changes to the base code from commits that make changes to the SHA-1 of the submodules. Having these commits separated you can bring them to the master branch using the Cherry-pick command, as Lucas suggested.

  • I did not give the status completely, but it actually looks like this: -base has 2 submodules: -Sub1 has 1 submodule: -base has 1 submodule: --resouces -sub2 has 1 submodule: -base has 1 submodule: -resouces This is the original configuration, but the base is the only one that is shared between projects and has, in each project, a different Resources.

0

Tip: Create a new branch from the "project1" branch, then remove the submodule from that new branch, and then finally merge the new branch into the master branch (and then "project2").

Sort of like this:

git checkout projeto1
git branch temporario
git checkout temporario

git submodule deinit caminho/do/submodulo
git rm caminho/do/submodulo
git commit -m "Submódulo removido"

git checkout master
git merge temporario
git branch -d temporario

git checkout projeto2
git merge master
  • I will test this alternative. It seemed to me the simplest to update the master.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.