2

I am using git submodules for the first time; so this might be obvious but I don't know how to solve it yet.

Background : I am creating a common repository to be included as a submodule for multiple projects. These projects and the submodule have several development branches and I would like a specific branch of the project to point to a specific branch in the submodule. I don't mind setting up CI with a script to automate this. For example, branches for,

Project A : master, A-1, A-2

Project B : master, B-1, B-2

Submodule : master, A-1, A-2, B-1, B-2

Questions : Is this even possible? Is submodule the right choice for this case? If yes, how do I set it up?

Thanks in advance!

Samarth
  • 119
  • 1
  • 1
  • 4

2 Answers2

0

It is possible, provided you are declaring your submodule with a branch to follow

git submodule add -b <branch> <repository> [<path>]. 

From there, in each of your project, a simple git submodule update --remote will update the content of the submodule to the latest HEAD from <repository>/<branch>.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

You can also use git subtree (an alternative way for git submodule to add a single branch).

For the way to add submodule's branch into different repo

You just need to use below command to add a submodule repo's branch into a folder of the main repo:

git subtree add --prefix=<folder name> <URL for the submodule repo> <branch name>
  • Such as add submodule master branch to projectA’s master branch, you can use below commands:

    # In local project A repo
    git checkout master
    git subtree add --prefix=master <URL for the submodule repo> master
    git push origin master
    
  • To add submodule B-1 branch to projectB’s B-1 branch, you can use below commands:

    # In local project B repo
    git checkout B-1
    git subtree add --prefix=B-1 <URL for the submodule repo> B-1
    git push origin B-1
    

For the way to update subtree in the main repos:

If the submodule's branch has updated with new changes, you can update the subtree existing in the main repos by:

git subtree pull --prefix=<folder name> <URL for the submodule repo> <branch name>
  • Such as if submodule B-1 branch update with new changes, you can update the B-1 folder in projectB correspondingly as below:

    # In local project B repo
    git checkout B-1
    git subtree pull --prefix=B-1 <URL for the submodule repo> B-1
    git push origin B-1
    

For the way to push changes from main repos' subtree to submodule repo

If you want to push changes to submodule repo directly from the main repos' subtree, you can use the command:

git subtree push --prefix=<folder name> <URL for the submodule repo> <branch name>
  • Such as push changes from ProjectA A-1 branch (A-1 folder) to submodule repo, you can use the commands below:

    # In local project A repo
    git checkout A-1
    # Make the commit changes in the folder A-1
    git subtree push --prefix=A-1 <URL for the submodule repo> A-1
    git push origin A-1
    
Marina Liu
  • 36,876
  • 5
  • 61
  • 74
  • Thanks for explaining the Subtree approach in depth. This is essentially what I was trying to achieve here. Do you see any clear disadvantage in using a Subtree against a Submodule? – Samarth Jul 17 '18 at 18:21
  • @Samarth No, there has no disadvantages for subtree by comparing with submodule. And the most situations to use git subtree abd submodule as below: to add **a single branch** from a git repo to another -- use gitsutree; to add **the whole git repo** to another git repo --use submodule. – Marina Liu Jul 18 '18 at 08:20