1

I am trying to automate a recurring workplace scenario.

I have a repo myRepo which contains a submodule mySub. I create a new branch for myRepo called myRepoBranch and also a new branch for mySub called mySubBranch. I commit changes in both branches and submit them for a pull request.

Issues arise where code in myRepoBranch directly depends on code in mySubBranch. If after submitting my pull request I checkout master and run git pull and git submodule update, mySub will no longer point to mySubBranch. This behaviour is expected and is perfectly fine for master. But if I need to checkout myRepoBranch again, I will also have to manually checkout mySubBranch for the submodule.

Is there a way to automate this manual submodule checkout? Is it possible to bind a specific submodule branch to a specific main repo branch?

IDDQD
  • 3,543
  • 8
  • 29
  • 40

1 Answers1

0

In git 1.8.2+ git submodule add -b branch_name URL_to_Git_repo optional_directory_rename

git 2.10+ added a great feature.

The name of the branch is recorded as submodule..branch in .gitmodules for update --remote. A special value of . is used to indicate that the name of the branch in the submodule should be the same name as the current branch in the current repository.

git submodule add -b . URL_to_Git_repo optional_directory_rename

# update your submodule
git submodule update --remote 

How can I specify a branch/tag when adding a Git submodule?

EncryptedWatermelon
  • 4,788
  • 1
  • 12
  • 28
  • 1
    Watch out for HTML characters, the text got rendered as `submodule..branch` rather than `submodule..branch` as StackOverflow markdown thinks `` is a bogus HTML directive and strips it from the generated result... – torek May 02 '19 at 20:06