Transfer git submodule to root repo with history

To reintegrate existing git submodule with it history tracking do following.

Remove existing submodule from working copy and remove it tracking from .gitmodules:

git rm --cached sub 
git rm .gitmodules #or just rm specific lines if you have more than one module 
rm -rf sub 
git commit -m "sub removed"

Add submodule url as another remote for root module and fetch it to branch:

git remote add submodule_origin git://url/to/submodule/origin 
git fetch submodule_origin

Move submodule date to subdirectory:

git checkout -b sub_master submodule_origin/master 
git clean  -fd
mkdir submodule 
git mv submodule_file1 submodule_file2 submodule 
git commit -m "moved submodule to subdir"

Merge submodule branch:

git checkout master 
git merge sub_master 
git commit -m "merged submodule"

Cleanup:

git branch -D sub_master 
git remote rm submodule_origin

4 thoughts on “Transfer git submodule to root repo with history

    1. So with a little help of ls -l and TextMate I could replace your

      git mv submodule_file1 submodule_file2 submodule

      with lots of

      git mv "/folder" "submodule/folder"
      git mv "/folder/foo.swift" "submodule/folder/foo.swift"
      ...

      and all went fine. THANKS for the post. Really helpful!

    2. So with a little help of ls and TextMate I could replace your

      git mv submodule_file1 submodule_file2 submodule

      with lots of

      git mv "/folder" "submodule/folder"
      git mv "/folder/foo.swift" "submodule/folder/foo.swift"
      ...

      and all went fine. THANKS for the post. Really helpful!

      1. One last comment (sorry you can delete the previous comment with ls -l): It’s important to ignore the fatal: bad source that the terminal spits 😉 Not sure why but it still worked with this error.

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax