Search code examples
gitgit-submodules

Merge git submodule into main repository


Ok, we have a repository with 3 submodules. Now we want to merge this submodules back into main repository, keeping all history (well, using submodules turned out to be more headache than being usefull). How do we proceed?


Solution

  • Let's say you have the following filesystem (and let's assume you only have one submodule, to simply the answer)

    myRepoRoot
    ├── myMainFiles/
    └── submodule/
    

    You just need to do:

    # Preparing the filesystems
    cd submodule
    # "submodule" is basically the path where you need your submodule code to be in the new repository,
    # e.g. app/code/x/y (<-- in this case you need to create multiple folders ofc, not just one like seen here)
    mkdir submodule
    git mv file1 file2 dir1 dir2 submodule 
    git commit -am "Moved file"
    
    # Actually merging
    cd myRepoRoot
    git remote add sub url_to_submodule
    git fetch sub
    git merge sub/master
    

    To explain it with words: you have several trees with no commit in common, and you just need to merge those trees. That's what the second part is doing.

    The first part is just needed to ensure the filesystem of the submodule will be what you could expect.