1

My project contains a submodule submodA. For the branch_dev the origin url of the submodule links to sub_dev which is a fork of sub_master but where I can apply modification and commit. For the branch_master the origin url of the submodule links direclty to sub_master

branch_dev/submodA--origin-> sub_dev --fork--> sub_master

master_dev/submodA--origin-> sub_master

By doing like this, I can clone branch_dev and find automatically the modification I did in sub_dev using git submodule update --init --recursive.

My problem is that the origin url of submodA which is different between my two branches doesn't change automatically and I have to change manually in the submodule the origin url with git remote set-url origin sub_dev or sub_master.

Does it have a solution to automatically change the origin url of my submodule when switching between branches ? I may try something too complicated so I would also be glad to here others solutions for my situation.

Katawan
  • 11
  • 1

1 Answers1

0

IMO you don't need to change origin's URL at all. Add two different remotes (one of them can be called origin but that doesn't matter) and set different upstreams to every branch. Something like this:

cd submodA
git branch --set-upstream-to=origin master
git branch --set-upstream-to=origin2 dev

Now you can git push/pull in the submodule without explicitly naming remote, so git uses the upstream of the currently checked out branch.

phd
  • 82,685
  • 13
  • 120
  • 165
  • Thank you for answering. In `submodA` I only have one branch `master` which has two different remotes according to the branch of the project I chose. If I create two branches in `submodA` I guess I would need to checkout the branch I want to use every time I switch between `master_dev` and `branch_dev` ? My goal would be to have only one branch in `submodA` which fetch the good remote `sub_dev` or `sub_master` according two the branch projet. Like this, people who clone my repo will automatically have the appropriate submodule by running `git submodule update --init --recursive` – Katawan Feb 07 '18 at 04:34
  • 1
    I finally found what I was looking for [here](https://stackoverflow.com/questions/913701/changing-remote-repository-for-a-git-submodule). I had already modified the .gitmodules file but the command `git sync` was the missing piece of the puzzle – Katawan Mar 05 '18 at 11:13