Restore git submodules from .gitmodules

I have a folder, which was a git repo. It contains some files and .gitmodules file. Now, when I do git init and then git submodule init, the latter command output is nothing. How can I help git to see submodules, defined in .gitmodules file without running git submodule add by hand again?

this is my .gitmodules file:

  • Force git to add dotfiles to repository
  • Planning repository layout for git migration
  • How to Clone a TFS Repository with a Hyphen in the name Using git-tf
  • git branch permissions
  • Is there a way to force the format of a README.txt file on github?
  • How to `git add` non-recursively?
  • [submodule "vim-pathogen"]
        path = vim-pathogen
        url = git://
    [submodule "bundle/python-mode"]
        path = bundle/python-mode
        url = git://
    [submodule "bundle/vim-fugitive"]
        path = bundle/vim-fugitive
        url = git://
    [submodule "bundle/ctrlp.vim"]
        path = bundle/ctrlp.vim
        url = git://
    [submodule "bundle/vim-tomorrow-theme"]
        path = bundle/vim-tomorrow-theme
        url = git://

    and here is listing of this dir:

    drwxr-xr-x  4 evgeniuz 100 4096 июня  29 12:06 .
    drwx------ 60 evgeniuz 100 4096 июня  29 11:43 ..
    drwxr-xr-x  2 evgeniuz 100 4096 июня  29 10:03 autoload
    drwxr-xr-x  7 evgeniuz 100 4096 июня  29 12:13 .git
    -rw-r--r--  1 evgeniuz 100  542 июня  29 11:45 .gitmodules
    -rw-r--r--  1 evgeniuz 100  243 июня  29 11:18 .vimrc

    so, definitely, it is in top level. the git directory is not changed, only git init is done

  • Using two git repos in one folder
  • Modify history of a git topic branch that used merges to use rebases
  • Git - restore a deleted folder from previous commit
  • Git diff with remote tag
  • git: better way for git revert without additional reverted commit
  • Merge branches without checking out branch
  • 3 Solutions collect form web for “Restore git submodules from .gitmodules”

    git submodule init only considers submodules that already are in the index (i.e. “staged”) for initialization. I would write a short script that parses .gitmodules, and for each url and path pair runs:

    git submodule add <url> <path>

    For example, you could use the following script:

    set -e
    git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
        while read path_key path
            url_key=$(echo $path_key | sed 's/\.path/.url/')
            url=$(git config -f .gitmodules --get "$url_key")
            git submodule add $url $path

    This is based on how the script itself parses the .gitmodules file.

    Expanding on @Mark Longair’s answer, I wrote a bash script to automate steps 2 & 3 of the following process:

    1. Clone a ‘boilerplate’ repo to begin a new project
    2. Remove the .git folder and re-initialize as a new repo
    3. Re-initialize the submodules, prompting for input before deleting folders

    set -e
    rm -rf .git
    git init
    git config -f .gitmodules --get-regexp '^submodule\..*\.path$' > tempfile
    while read -u 3 path_key path
        url_key=$(echo $path_key | sed 's/\.path/.url/')
        url=$(git config -f .gitmodules --get "$url_key")
        read -p "Are you sure you want to delete $path and re-initialize as a new submodule? " yn
        case $yn in
            [Yy]* ) rm -rf $path; git submodule add $url $path; echo "$path has been initialized";;
            [Nn]* ) exit;;
            * ) echo "Please answer yes or no.";;
    done 3<tempfile
    rm tempfile

    Note: the submodules will be checked out at the tip of their master branch instead of the same commit as the boilerplate repo, so you’ll need to do that manually.

    Piping the output from git config into the read loop was causing problems with the prompt for input, so it outputs it to a temp file instead. Any improvements on my first bash script would be very welcome 🙂

    Big thanks to Mark,, bash: nested interactive read within a loop that's also using read, and tnettenba @ for helping me arrive at this solution!

    I had a similar issue. git submodule init was failing silently.

    When I did:

    git submodule add <url> <path>

    I got:

    The following path is ignored by one of your .gitignore files: ...

    I’m thinking that .gitignore(d) paths might be the cause.

    Git Baby is a git and github fan, let's start git clone.