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?
Asked
Active
Viewed 4.3k times
63
-
I found a solution in SO that worked for me: http://stackoverflow.com/questions/9035895/how-do-i-add-a-submodule-to-a-sub-directory – ItayB Mar 15 '16 at 13:37
3 Answers
45
I ended up doing this:
- Create a
submodules
directory. - Add the submodule in this directory.
- 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.

gitaarik
- 42,736
- 12
- 98
- 105
-
hmmm I don't know if this will fly with my Windows developers but we shall try it and tell – fixmycode Jun 27 '17 at 23:51
-
4@CaptainMan this works if you install GitBash with the "Enable Symbolic Links" permission! – fixmycode Feb 22 '19 at 16:50
-
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

VonC
- 1,262,500
- 529
- 4,410
- 5,250
-
1I found this note in your [subtree merge strategy](https://mirrors.edge.kernel.org/pub/software/scm/git/docs/howto/using-merge-subtree.html) particularly relevant: `Also, in case you make changes to the other project, it is easier to submit changes if you just use submodules.` – Devin Rhode Aug 19 '21 at 00:13
-
1
-
Links pages tend to disappear much more frequently than you think. it would be nice if you could add the actual example here. – AaA Oct 22 '22 at 02:24
-
@AaA Very true. I have included the relevant (and updated) examples in the answer. – VonC Oct 22 '22 at 08:55
-1
- Create new branch
- Move required files to new branch
- Add sub module to super repository
- Checkout sub-module to new branch

Simson
- 3,373
- 2
- 24
- 38