How can I add a specific folder from a git repo as a git submodule?
Asked Answered
S

3

65

I have a git repo and I want to add a submodule to it. Problem is, the submodule exists as a folder inside another repo. Can I add only that folder as a submodule?

Spelldown answered 1/7, 2010 at 14:13 Comment(1)
I found a solution in SO that worked for me: #9036395Hydrostat
H
29

If you really need to include part of an other repository within the history of your own repo, then the subtree merge strategy is more adequate than submodules.

  • name the other project "repo1", and fetch.
  • prepare for the later step to record the result as a merge.
  • read "main" branch of repo1 to the subdirectory "repo1".
  • record the merge result.
  • maintain the result with subsequent merges using "subtree"
$ git remote add -f repo1 /path/to/repo1
$ git merge -s ours --no-commit --allow-unrelated-histories repo1/main
$ git read-tree --prefix=repo1/ -u repo1/main
$ git commit -m "Merge repo1 as our subdirectory"
$ git pull -s subtree repo1 main

But in both case, the full repository is linked to your repo, not just one directory.
And partial cloning is not possible.

You could try and isolate that directory in its own repository, and then add it as a submodule, but that means its history will be totally separated from the repo its was coming from originally.

The modern example would use git filter-repo

cd /path/to/repo1
git filter-repo --path repo1SubFolder

#Move the files inside repo1SubFolderto the root
git filter-repo --subdirectory-filter repo1SubFolder/

# Go to your new repository, add a remote to the original repository
cd /path/to/repo2
git remote add repo1 /path/to/repo1

# Pull files and history from this branch into repo2 
# (containing only the directory you want to move) .
git pull repo1 main
git remote rm repo1
Harod answered 1/7, 2010 at 14:39 Comment(4)
I found this note in your subtree merge strategy particularly relevant: Also, in case you make changes to the other project, it is easier to submit changes if you just use submodules.Dimorphism
@DevinRhode 11 years later, I agree. I mostly use submodules these days.Harod
Links pages tend to disappear much more frequently than you think. it would be nice if you could add the actual example here.Fregger
@Fregger Very true. I have included the relevant (and updated) examples in the answer.Harod
F
48

I ended up doing this:

  1. Create a submodules directory.
  2. Add the submodule in this directory.
  3. Create a symlink to the specific directory inside the submodule.

This way you have default Git submodule behaviour and in your project you only use a subset of the whole submodule.

Flowerpot answered 3/9, 2013 at 10:36 Comment(3)
hmmm I don't know if this will fly with my Windows developers but we shall try it and tellObscurantism
@CaptainMan this works if you install GitBash with the "Enable Symbolic Links" permission!Obscurantism
Excellent. Except antora/asciidoctor doesn't support symlinks :(Monetta
H
29

If you really need to include part of an other repository within the history of your own repo, then the subtree merge strategy is more adequate than submodules.

  • name the other project "repo1", and fetch.
  • prepare for the later step to record the result as a merge.
  • read "main" branch of repo1 to the subdirectory "repo1".
  • record the merge result.
  • maintain the result with subsequent merges using "subtree"
$ git remote add -f repo1 /path/to/repo1
$ git merge -s ours --no-commit --allow-unrelated-histories repo1/main
$ git read-tree --prefix=repo1/ -u repo1/main
$ git commit -m "Merge repo1 as our subdirectory"
$ git pull -s subtree repo1 main

But in both case, the full repository is linked to your repo, not just one directory.
And partial cloning is not possible.

You could try and isolate that directory in its own repository, and then add it as a submodule, but that means its history will be totally separated from the repo its was coming from originally.

The modern example would use git filter-repo

cd /path/to/repo1
git filter-repo --path repo1SubFolder

#Move the files inside repo1SubFolderto the root
git filter-repo --subdirectory-filter repo1SubFolder/

# Go to your new repository, add a remote to the original repository
cd /path/to/repo2
git remote add repo1 /path/to/repo1

# Pull files and history from this branch into repo2 
# (containing only the directory you want to move) .
git pull repo1 main
git remote rm repo1
Harod answered 1/7, 2010 at 14:39 Comment(4)
I found this note in your subtree merge strategy particularly relevant: Also, in case you make changes to the other project, it is easier to submit changes if you just use submodules.Dimorphism
@DevinRhode 11 years later, I agree. I mostly use submodules these days.Harod
Links pages tend to disappear much more frequently than you think. it would be nice if you could add the actual example here.Fregger
@Fregger Very true. I have included the relevant (and updated) examples in the answer.Harod
A
-2
  1. Create new branch
  2. Move required files to new branch
  3. Add sub module to super repository
  4. Checkout sub-module to new branch
Appointive answered 25/9, 2019 at 4:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.