git: Split existing repository into submodules
Asked Answered
C

2

7

I only found answers on how to use git subtrees to split up a repository. However, I explicitly want submodules.

It's a Java maven project. Currently, everything is in one maven project and one repository. My goal is to achieve something like this:

The root repository should contain the main pom.xml, system documentation, etc Then there should be a few submodules, one for a utility library, one for the main application, and so on. The submodules are their own maven project, referenced from the main maven project in the root repository. The root repository will not contain any source code.

I could create everything new from current HEAD, but it is important to me that the commit history is kept as complete as possible.

Chromatolysis answered 9/12, 2018 at 18:10 Comment(0)
S
14

I only found answers on how to use git subtrees to split up a repository. However, I explicitly want submodules.

Thats exactly what u need to do. Split the "main" into branches with git subtree split <path> -b <branch> and then add remote for each submodule and push the branch to the remote.

# split the "main repo"
git subtree split -P path -b <branch1>

# For each branch that you extract

# add remote for branch 1
git remote add submodule1 <url>

# push the submodule
git push submodule1 <branch>

Once you have all your submodules set up add them to the "main" repo

# add the submodules 
git submodule add <url>

# and once all your submodules are added commit the .gitmodules file
Sericeous answered 9/12, 2018 at 18:51 Comment(6)
Maybe I'm just not seeing it, but where do I actually create a submodule? Skimming over the documentation I think I need git submodule add ... for each extracted subtree?Chromatolysis
This I need git submodule add ... ... this is the way to "add" your submodule to your oroject, the code above shows you how to create themSericeous
What is url exactly ? I know one url, the one of my remote git repository. So should I create a specific url for each module that doesn't exist yet ???Knocker
yep. Exactly, url per each submoduleSericeous
OK thanks, that is what I didn't understand, I need to create the remote repositories on a git server firstKnocker
Of course. Create a repo for each module and then add it to giSericeous
C
5
# split the "main repo"
git subtree split -P path -b <branch>

# Create your repository, and get git url

# add remote for branch
git remote add submodule <url>

# push the submodule
git push -u submodule <branch>:master

# remove path
git rm -r path

# Stage and commit changes
git add -A
git commit -m 'Remove <path> for submodule replacement'

# add the submodule 
git submodule add <url> <path>

# and once your submodule is added commit the .gitmodules file 
Canalize answered 11/4, 2020 at 3:49 Comment(2)
Awesome! thanks for the useful answer. I have 2 branches in my repository. I did this submodule splitting on master, but I am having trouble merging with my dev branch. After splitting, if I try git checkout dev, I get: error: The following untracked working tree files would be overwritten by checkout: Please move or remove them before you switch branches. Any idea how to fix this?Menell
@Menell My guess would be that you need to rm -rf <path that git is trying to checkout>, because git won't overwrite something that is "untracked". Your dev branch has those files tracked, whereas on master they're submodules and thus "not tracked" internally in the same way (is my guess). If you're iffy about rm -rf (as you should be), take a backup of the whole project folder first. Also, I realize this is going on four years old, but if I ended up here, someone else might as well.Haemato

© 2022 - 2024 — McMap. All rights reserved.