Add a list of submodules to the git

I have cloned a project and customized it. the project is using some extra projects as submodules. I have setup mine git repository and push the main project there. but I did not add the submodules to mine git project.

I want to pull the submodules to my project. I know using following command I can add submodules one by one :

git submodule add <sub-m url> <path>

But they are a lot. is there another way to add them automatically and all together ? here is the list of submodules :

submodule "submodules/bcg729"]
    url = git://git.linphone.org/bcg729.git
[submodule "submodules/bctoolbox"]
    url = git://git.linphone.org/bctoolbox.git
[submodule "submodules/belcard"]
    url = git://git.linphone.org/belcard.git
[submodule "belle-sip"]
    url = git://git.linphone.org/belle-sip.git
[submodule "submodules/belr"]
    url = git://git.linphone.org/belr.git
[submodule "submodules/bzrtp"]
    url = git://git.linphone.org/bzrtp.git
[submodule "submodules/cmake-builder"]
    url = git://git.linphone.org/linphone-cmake-builder.git
[submodule "submodules/externals/antlr3"]
    url = git://git.linphone.org/antlr3.git
[submodule "submodules/externals/axmlrpc"]
    url = git://git.linphone.org/axmlrpc.git
[submodule "submodules/externals/bv16-floatingpoint"]
    url = git://git.linphone.org/bv16-floatingpoint.git
[submodule "submodules/externals/cunit"]
    url = git://git.linphone.org/cunit.git
[submodule "submodules/externals/ffmpeg"]
    url = git://git.linphone.org/ffmpeg.git
[submodule "submodules/externals/gsm"]
    url = git://git.linphone.org/gsm.git
[submodule "submodules/externals/libmatroska"]
    url = git://git.linphone.org/libmatroska-c.git
[submodule "submodules/externals/libupnp"]
    url = git://git.linphone.org/libupnp.git
[submodule "submodules/externals/libvpx"]
    url = https://chromium.googlesource.com/webm/libvpx
[submodule "submodules/externals/libxml2"]
    url = git://git.gnome.org/libxml2.git
[submodule "submodules/externals/mbedtls"]
    url = git://git.linphone.org/mbedtls.git
[submodule "submodules/externals/opencore-amr"]
    url = git://git.linphone.org/opencore-amr.git
[submodule "submodules/externals/openh264"]
    url = https://github.com/cisco/openh264
[submodule "submodules/externals/opus"]
    url = git://git.linphone.org/opus.git
[submodule "submodules/externals/speex"]
    url = git://git.linphone.org/speex.git
[submodule "submodules/externals/srtp"]
    url = git://git.linphone.org/srtp.git
[submodule "submodules/externals/vo-amrwbenc"]
    url = git://git.linphone.org/vo-amrwbenc.git
[submodule "submodules/externals/x264"]
    url = git://git.linphone.org/x264.git
[submodule "submodules/linphone"]
    url = git://git.linphone.org/linphone.git
[submodule "submodules/msamr"]
    url = git://git.linphone.org/msamr.git
[submodule "submodules/mscodec2"]
    url = git://git.linphone.org/mscodec2.git
[submodule "submodules/msopenh264"]
    url = git://git.linphone.org/msopenh264.git
[submodule "submodules/mssilk"]
    url = git://git.linphone.org/mssilk.git
[submodule "submodules/mswebrtc"]
    url = git://git.linphone.org/mswebrtc.git
[submodule "submodules/msx264"]
    url = git://git.linphone.org/msx264.git
[submodule "linphone"]
    url = git://git.linphone.org/linphone.git

I don't think there is such a way with git. However, you could just do a shell script that iterates your list and calls git submodule add on each of them, one by one.


Why just copying them to .gitmodules or to .git/index won't do the trick:

The "git submodule add" command does a couple of things:

  • It clones the submodule under the current directory and by default checks out the master branch.
  • It adds the submodule's clone path to the ".gitmodules" file and adds this file to the index, ready to be committed.
  • It adds the submodule's current commit ID to the index, ready to be committed.
  • (source)

    So you would be missing the last step, and git submodule init/update expect the commit id to be already in place. That is why you need git submodule add .


    Based on @eis suggestion, you have to create a script to make all the git add commands.

    A way to do that is to create the .gitmodules containing your needs, then parse it with a script like this one:

    iterator=1;
    subpaths=$(git config --file .gitmodules --get-regexp path | awk '{ print $2}');
    subrepos=$(git config --file .gitmodules --get-regexp url | awk '{ print $2}');
    for path in $subpaths; do
      repo=$(echo "$subrepos"| sed $iterator'q;d');
      git submodule add $repo $path;
      let iterator++;
    done
    
    链接地址: http://www.djcxy.com/p/92396.html

    上一篇: 从存储库中检索单个文件

    下一篇: 将子模块列表添加到git