Git merge between branches with submodules

4

I have a following mobile project. A repository where code is shared between multiple projects. I'll call this base repository.

In the projects were created branches of this base repo because it was necessary to add a different submodule for each project.

Eg: Project 1. The project1 branch is created in the base repository and the sub1 module sub1 is added. Project 2. Created the project2 branch in the base repository and added the sub2 sub-module.

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

In the master branch and running a git merge project1, it happens that the sub-module of project1 is included in the master, and this can not occur because the master is only to serve as the basis for the projects.

How to solve this?

    
asked by anonymous 06.02.2014 / 15:59

4 answers

2

First, merge with parameter --no-commit :

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

At this point, the merge will have been made, but the commit has not yet been committed. Take advantage now to remove the submodule:

git rm caminho/do/submodulo

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

Once this is done, continue with the merge using git commit .

Ready. The merge will include removing the submodule.

You will then be able to do merges normally as long as no changes have been made to the submodule. Submodule changes are likely to result in merge conflicts, which will allow you to intercede at the time of merge. If this is not the case, you will have to repeat the above procedure whenever there are changes in the submodule.

    
07.02.2014 / 08:25
3

The interesting thing in this case is whenever you make a change, create a branch from the master because from there 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 project1, project2, and master. After mergeing in the master you can even delete this new branch.

To solve your problem on time, I think the best solution is to use the cherry-pick command. In it you apply commits (regardless of which branch you are) in the current branch.

It would be something like:

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

Here is the documentation link on how to use it:

link

    
06.02.2014 / 16:44
0

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

More or less 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
    
06.02.2014 / 16:56
0

Perhaps a better way to organize yourself is to turn what you call the 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 this is not feasible, I suggest you rebase your commits in the branches, to separate the commits that make changes to the base code, from commits that make changes to the SHA-1 of the submodules. Having these separate commits you can bring them to the master branch using the cherry-pick command, as Lucas suggested.

    
07.02.2014 / 02:35