5

Is there a command to set the same branch name for all existing Git Submodules

git submodule add -b develop *

Basically I need a way to recursively set the branch for each module in the .gitmodules file.

MatPag
  • 41,742
  • 14
  • 105
  • 114
DarVar
  • 16,882
  • 29
  • 97
  • 146

2 Answers2

15

Looking for a way to recursive set the branch in the .gitmodules file

With Git 2.22 (Q2 2019, four years later), you will be able to use git submodule set-branch -b <abranch>, because git submodule learns set-branch subcommand that allows the submodule.*.branch settings to be modified.

See commit b57e811, commit c89c494 (08 Feb 2019), and commit 7a4bb55 (07 Feb 2019) by Denton Liu (Denton-L).
(Merged by Junio C Hamano -- gitster -- in commit 01f8d78, 25 Apr 2019)

submodule: teach set-branch subcommand

This teaches git-submodule the set-branch subcommand which allows the branch of a submodule to be set through a porcelain command without having to manually manipulate the .gitmodules file.

In your case, for all submodules, using git submodule foreach:

git submodule foreach 'git submodule set-branch --branch aBranch -- ${sm_path}'
git submodule foreach 'git submodule set-branch --default -- ${sm_path}'

(the last line set the master branch, which is the default)


Before Git 2.22, you would use the command I mentioned in "How can I specify a branch/tag when adding a Git submodule?"

 git submodule foreach 'git config -f .gitmodules submodule.${sm_path}.branch <branch>'

Note: Git 2.24 (Q4 2019) makes clear the --default and --branch options are mutually exclusive.

See commit 40e747e (16 Sep 2019) by Denton Liu (Denton-L).
(Merged by Junio C Hamano -- gitster -- in commit 7f17913, 07 Oct 2019)

Armen Michaeli
  • 8,625
  • 8
  • 58
  • 95
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    how do you get ${sm_path}? Is that from foreach? When I try your command: git submodule foreach 'git submodule set-branch --branch development -- ${sm_path}' I get this error: fatal: run_command returned non-zero status for api api is one of my submodules – Joshua Dyck May 16 '19 at 03:57
  • @JoshuaDyck It is set by `git submodule foreach`: https://git-scm.com/docs/git-submodule#Documentation/git-submodule.txt-foreach--recursiveltcommandgt. What version of Git are you using? On which OS and which shell? Are you using it from the root folder of the parent repo hosting those submodules? – VonC May 16 '19 at 04:30
  • I installed the latest from git-scm.com which was 2.21. That must be it. I assumed the latest release would have it. But as you stated above it is 2.22. How would you go about getting 2.22 if git-scm.com only has 2.21? Thanks! – Joshua Dyck May 17 '19 at 19:47
  • @JoshuaDyck Correct. You now have the 2.22rc0 for you to test. – VonC May 17 '19 at 19:50
  • hi @VonC I just installed Git 2.23 in windows and have same problems as Joshua... – YaP Nov 04 '19 at 15:25
  • @YaP Can you try with a simple echo? I did so two days ago without issue in https://stackoverflow.com/a/58667195/6309, as a concrete example. – VonC Nov 04 '19 at 17:11
  • how can I leverage the --depth 1 here ? I cant get this to work – Martin Kosicky Apr 28 '20 at 13:54
  • @MartinKosicky What version of Git are you using? And what " I cant get this to work" means? Is there an error message? – VonC Apr 28 '20 at 14:11
  • It seems that it still is trying to fetch master, unles i invoke git submodule update --remote . therefore the correct branch has to be upfront pulled (and --depth 1 strips that out on the first update --init) – Martin Kosicky Apr 28 '20 at 14:49
  • @VonC more importantly, I cannot get the right combination of operations to work to track remote branch on submodule with shallow init, I am using git submodule update --remote --init --depth 1 , but that doesnt work with --depth 1, without it it works. I have newest git i think 2.26.2 – Martin Kosicky Apr 28 '20 at 14:51
  • @MartinKosicky OK. Can you ask that as a separate question? – VonC Apr 28 '20 at 15:01
  • @VonC oki https://stackoverflow.com/questions/61483547/how-to-shallow-pull-submodule-that-is-tracked-by-branch-name – Martin Kosicky Apr 28 '20 at 15:09
4

See git submodule foreach.

Evaluates an arbitrary shell command in each checked out submodule.

git submodule foreach git checkout -b develop
Jiří Pospíšil
  • 14,296
  • 2
  • 41
  • 52