Search code examples
gitgit-submodules

Automatically Add All Submodules to a Repo


I have a big Directory ~/.vim and in it I have a subdirectory with many other git repos in it. I want to make a git repo of my ~/.vim directory though, but don't want to go through each of my other git subdirectories.

Is there any way of just recursively going through and adding all submodules?


Solution

  • Suppose that .vim is already a valid git repo and your want to add all git repos to your main git repo, then the for loop below is probably what you need:

    First, cd to the root of your git repository.

    Paste-able preview command- echo only, won't make any changes:

    for x in $(find . -type d) ; do if [ -d "${x}/.git" ] ; then cd "${x}" ; origin="$(git config --get remote.origin.url)" ; cd - 1>/dev/null; echo git submodule add "${origin}" "${x}" ; fi ; done
    

    Paste-able command to add the submodules:

    for x in $(find . -type d) ; do if [ -d "${x}/.git" ] ; then cd "${x}" ; origin="$(git config --get remote.origin.url)" ; cd - 1>/dev/null; git submodule add "${origin}" "${x}" ; fi ; done
    

    This loop first finds directories only, looks for a .git directory, identifies the original URL and then adds the submodule.

    Readable version:

    for x in $(find . -type d) ; do
        if [ -d "${x}/.git" ] ; then
            cd "${x}"
            origin="$(git config --get remote.origin.url)"
            cd - 1>/dev/null
            git submodule add "${origin}" "${x}"
        fi
    done