5

I add a submodule using following command

git submodule add -b br1 [repo]

The .gitmodules file has entry branch=br1 . Now I want to switch the branch to br2. I can go into submodule folder and switch the branch but that does not update .gitmodules file.

How I can do that?

2
  • Why do you want to do this? If you think about it, if you change the branch which the submodule is tracking, then wouldn't that possibly throw off everyone else's code? Submodules is one area of Git which is very tricky. There might be a workaround to your problem. Commented Sep 7, 2015 at 9:25
  • 2
    I am working on a stable branch br1 and want to test on experimental br2. If I do not update .gitmodules files other users will not be able to see it. Commented Sep 7, 2015 at 9:28

2 Answers 2

3

Git uses .gitmodules for storing a link to your submodule project, but not a particular version of it. So when you switch branch of the submodule, git .gitmodules file is not changed.

Instead git updates commit number of your submodule version when you change the submodule. If you look at your upper module

git diff

you'll see something like

-Subproject commit 829b869657418fdac7964c3671ed9a378f09c032
+Subproject commit 829b869657418fdac7964c3671ed9a378f09c032-dirty

If you wish everybody uses new submodule branch, you have to commit & push that change (new submodule commit number) into your upper module repo.

Basically it works like a link to the particular submodule version.

Look here for details: https://git-scm.com/book/en/v2/Git-Tools-Submodules#Starting-with-Submodules

Sign up to request clarification or add additional context in comments.

2 Comments

I did not see the diff because I created a branch and that does not do anything other than pointer!
But still, this is about the commit. If you want to update the commit and change the branch, is the correct workflow to edit the .gitmodules file directly?
0

I faced the same issue. This is how I solved.

Because the main repo tracked a branch, not a specific commit, pulling changes in the submodule or switching branch makes no modification in the main repo.

Instead, edit the .gitmodules file. There is a line

branch = br1

Replace it with the new branch (after at least a fetch in the submodule)

branch = br2

Then in a new commit, you can describe that br2 tracked from now on.

Then any other developer can update their clone with git submodule update --remote to pull you changes, and track br2.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.