1

I have a repo X (let us assume it has branches XB1, XB2, and I have checked out branch XB1). I have added a submodule Y (let us assume this submodule has branches YB1, YB2). I have added the submodule Y (branch YB1) in X.

Is there a way to add branch YB2 as submodule in branch XB2?

In summary, what I am looking for is that when I checkout XB1, I want to use YB1 and when I checkout XB2 I want to use YB2.

Not sure if the above is possible in git, but thought of asking.

user376507
  • 2,004
  • 1
  • 19
  • 31
  • 1
    it's definitely possible. with XB1 all set up, check out XB2, navigate into the submodule and checkout YB2. navigate to the main repo and run git status and it should show that there's been a change to the submodule. you can stage and commit that change, which is really just a change to the file .gitmodules that same way you'd commit any change – Hugh_Kelley Dec 03 '20 at 20:59
  • here's a full answer https://stackoverflow.com/questions/39672720/checkout-branch-in-git-submodule – Hugh_Kelley Dec 03 '20 at 21:00
  • https://stackoverflow.com/search?q=%5Bgit-submodules%5D+switch+branch+checkout – phd Dec 04 '20 at 00:50
  • Your primary tool should be [`git checkout --recurse-submodules`](https://stackoverflow.com/a/43854593/7976758). The second way to try is [`post-checkout` hook](https://stackoverflow.com/q/55631116/7976758). – phd Dec 04 '20 at 00:52

1 Answers1

0

Yes, it is possible. When you add submodule, there is possibility to set which branch is tracked. For example, check this tutorial, chapter 3.1.

But, the issue could be while merging your branches. Your both branches will use different submodules actually - so merge conflicts are possible.

If you are not going to merge them, but just want to keep as separate development targets, then rather consider create fork of the repository and keep development for each of the versions separately.

kosist
  • 2,868
  • 2
  • 17
  • 30
  • 1
    While you can set a branch name at `git submodule add` time, it's basically unused. The submodule code doesn't work with branch names; it works with raw commit hash IDs. – torek Dec 04 '20 at 00:22