Shad
Shad

Reputation: 1029

Different git submodules pointing to different tags/branches from same repository

The scene is I have a git project A and it has a submodule pointing to project B repo, i.e. to a specific tag from such repo. I would like to create a branch in A that could point to a different tag from B. Questions: Is it possible? If yes, how? If no, I would like listen to your suggestions about a better project structure to allow this. Thanks in advance.

Edit: Without to have cloning repo B again.

Upvotes: 0

Views: 1182

Answers (1)

Mau76
Mau76

Reputation: 145

In short: Yes! This is definitely possible, but first a little clarification.

How git stores references to submodules

A git submodule (project B in your example) is always referenced by the main git project (A) by specifying a commit (its SHA-1 hash). The commit you are pointing to may also correspond to a tag (or a branch head), just remember that git stores the referenced hash. So, if you (for any reason) delete your tag in project B or even move it, project A will keep on referencing the initial commit.

Create a branch in the main project that points to a different commit in the submodule

Let's assume this: project A in its main branch (master) is referencing project B at a certain tag (tagB1). So you have already linked it as a submodule and it is stored into projB folder

  1. Create your branch in project A and switch to it as you would usually do.

    git checkout -b NewBranch

  2. Change to your submodule (projB) directory

    cd projB

  3. Switch (checkout) your submodule to the new commit or tag as desired

    git checkout tagB2

  4. Go back to project A main folder and commit it. This stores the link between project A (in the branch NewBranch) to the tag you have selected (actually the hash of the commit is stored, as explained)

Switching between main project branches and submodules

This is important to remember: if you switch your main project branch , the submodules will not automatically follow the main branch!! In our example, if you switch project A back to master branch, project B will remain to the version tagB2 and not tagB1 as you may think..

You need to run the command git submodule update

For more information check the ProGit chapter about submodules

Upvotes: 1

Related Questions