0

I have a git project named G, which has multi branches named A, B, C... Then I add a submodule S in branch A, update it, and commit all the changes. So I have a .gitmodule file and a 'directory' also named S lying on my working directory, as well as some changes in .git/config to track the module's info.

Then if I directly checkout branch B, git will define the 'directory' S as an untracked directory. In my understanding, this is caused because in branch B there is neither .gitmodule nor any other config files that can define S as a submodule, so git just think it as an untracked directory.

This is kind of annoying. If I remove the directory S in branch B, then when I checkout branch A again, the submodule S is also changed. But I don't want to add either submodule S or directory S to branch B (or to any other branches except A).

So my question is, is there some way to just add a submodule in one branch, while not affecting the other branches?

I know I can "git submodule deinit S" before I checkout B when I was in branch A. But I think this is not so convenient. I just want to use the files in another project, why should I init and deinit it every time? Or is my understanding to the usage of submodule wrong?

I also think about adding the directory S to .gitignore in other branches, but this is even more inconvenient if there's many branches.

And I also also think about just copying the project S files to the branch A, and commiting them. But then I cannot track the changes in project S directly.

And I also also also think about using symbolic link of project S, which finally meets my need. But I still want to know if there is a way in git to solve this problem.

(I'm using git 2.17.1

phd
  • 82,685
  • 13
  • 120
  • 165
  • If your branches are so different that one of them needs a submodule, but the other branch does not, maybe they should be two different repositories instead of branches. Do they share a large common codebase? Where else do the branches differ? – kowsky Dec 13 '18 at 14:26
  • Well, actually I'm writting a game-platform, and there is one branch to apply a specific new feature (remote-battle). This branch needs files in another project (mainly games written by others) to test. And there are also other features in developing, such as chating, ranking, all in different branches and some written by others. Really, these branches can be divided into different repositories, but it will be more difficult to cooperate and finally merge, I think. And no to mention the project, I still want to know whether there is a way to make submodule in one branch. – Porcupine Andrew Dec 13 '18 at 15:06
  • https://stackoverflow.com/a/43854593/7976758 – phd Dec 13 '18 at 16:25
  • 1
    *is my understanding to the usage of submodule wrong?* Not at all, everything is right. Submodules are quite inconvenient with branches. Either you add unneeded submodules to `.gitignore` or remove and checkout them; you can remove and re-checkout automatically in a `post-checkout` hook ([1](https://stackoverflow.com/a/37383406/7976758), [2](https://stackoverflow.com/a/50178825/7976758)). – phd Dec 13 '18 at 16:30
  • *And I also also think about just copying the project S files to the branch A, and committing them.* This can be done with [tag:git-subtree]. `git subtree` is an alternative for `git submodules`. – phd Dec 13 '18 at 16:34
  • @phd Thanks! I'm new to git, and your comments help me a lot. – Porcupine Andrew Dec 14 '18 at 02:22
  • Really, `git config --global submodule.recurse true` can perfectly solve this problem! Thanks for all the help. @kowsky @phd – Porcupine Andrew Dec 14 '18 at 08:05

0 Answers0