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?
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
Also, in case you make changes to the other project, it is easier to submit changes if you just use submodules.
–
Dimorphism 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.
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
Also, in case you make changes to the other project, it is easier to submit changes if you just use submodules.
–
Dimorphism - Create new branch
- Move required files to new branch
- Add sub module to super repository
- Checkout sub-module to new branch
© 2022 - 2024 — McMap. All rights reserved.