1

I have several repos within repos.
In repo3 I want to create several branches. When I move back a repo, i.e. in repo2, and do git branch, it shows me all the branches present in both repo2 and repo3. But what I want is to be able to create branches unique to their repos and and these branches shouldn't show up in repo2.

This diagram shows how I want:

   repo1/ repo2/ repo3 
            |      |
            |      |
            |      |_ _ _branchX, branchY, branchZ, branchK
            |
            |
          branchA, branchB, branchC
Deke
  • 4,451
  • 4
  • 44
  • 65
  • 1
    How many actual `.git` files do you have here? – Tim Biegeleisen Nov 14 '16 at 05:16
  • I have .git file only in repo1. – Deke Nov 14 '16 at 05:23
  • Look into using Git submodules. This would work as follows: Inside `repo2`, there would be a branch representing the submodule `repo3`. Inside `repo3`, you would then see all of its branches. – Tim Biegeleisen Nov 14 '16 at 05:25
  • thanks for information on submodule. This is how I did finally: I `git init` in each repo. Then I created new branches respective to that repo. When I moved a repo back and did `git branch`, it only showed branches present within that current repo. – Deke Nov 14 '16 at 05:41
  • Just wanted to add that this is not solved yet. Although it is possible to do gitinit in each repo, but this will give too many errors while pushing to github. Basically you have to add origins like this: `git remote add origin ssh://git@example.com:1234/myRepo.git` for each repo within repo. I'm now looking into git submodules suggestion. – Deke Nov 14 '16 at 06:33
  • 1
    Submodules sound like a good fit for the behavior you want. By the way, many frown upon submodules, so really the best answer might be to change your design so you don't even need them. – Tim Biegeleisen Nov 14 '16 at 06:34
  • 1
    Git doesn't "do" repos-within-repos. The nearest thing is *submodules*, but those are multiple *independent* repos. Well, sort-of-independent: a repo that refers to another repo as a "submodule" depends on that other repo existing, and having some specific commit (by hash ID). So if you ever remove the target of the submodule, the referring repo (the one with the submodule) is SOL. Up the creek without a paddle. Screwed, boned, had its mellow harshed... – torek Nov 14 '16 at 06:54

1 Answers1

0

This is how I did finally: I git init in each repo. Then I created new branches respective to that repo. When I moved a repo back and did git branch, it only showed branches present within that current repo.

Yes, but it also registered a gitlink (a special entry in the index) for the nested repo... without registering the nested repo remote url.
So when repo2 will make new commits and push them, repo1 will register the new repo2 SHA1 (gitlink), but won't know where that SHA1 is coming from (remote repo url).
Cloning again repo1 will result in an empty repo2 (and no repo3)

The proper way to initialize repo1 is by having repo2 and repo3 initialized separately, that is:

cd /path/to/repo2
git submodule add -- /url/repo3
git add .
git commit -m "Add repo3 as submodule"
git push

cd /path/to/repo1
git submodule add -- /url/repo2
git add .
git commit -m "Add repo2 as submodule"
git push

With that setting, a git branch done in repo1, 2 or 3 will only show the branches for the current repo.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250