Git branch as submodule to own repository

72 Views Asked by At

I have a git repository with the following structure:

repo - /folder1
     - /folder2
     ...

This repo has 2 branches:

repo ┌--------- master
     |
     └--------- branch1

I want to add branch1 as a submodule to this repo, so that I have the following structure:

repo - /folder1 - /branch1 (submodule)
     - /folder2
     ...

I know it is possible to add a submodule that tracks a branch instead of just a specific commit, but when I attempt adding the repo as its own submodule I can't commit the result, I get the following error:

fatal: git cat-file :0:folder1/branch1: bad file
Unable to checkout folder1/branch1

Running git-fsck returns a big list of dangling blobs.

To my knowledge I haven't found anyone trying to do this. I also haven't found anything in the documentation that says that this isn't possible.

Can anyone with more knowledge of git internals explain why this isn't possible/isn't working?

1

There are 1 best solutions below

3
On

Submodule can’t meet your requirement, you can use subtree instead.

When you add a submodule, the whole branches of the submodule repo are tracked by the main repo. So you can’t only track branch1 by submodule.

For subtree, it can add a branch to a certain path by:

git subtree add --prefix=folder1/branch1 <repo URL> branch1