2009-10-08 193 views
109

git子模块是否可以由其他几个git子模块构成,以及用于获取每个子模块内容的超级git回购?子模块内部的Git子模块(嵌套子模块)

我试图做到这一点,使用明显/天真的方式创建一个持有几个子模块的git仓库。

然后添加这个git回购到另一个git回购作为子模块。

然后尝试从超级git回购的根目录git submodule init然后git submodule update。但是这不能获取子子模块。

回答

149

正如Retrospectively add --recursive to a git repo

git submodule update --init --recursive 
提到

应该工作。

+8

这对我有用。请注意,我错误地认为'git submodule init; git submodule update --recursive'是上面的同义词,但事实并非如此。 – jsdalton 2012-03-21 16:43:12

+0

+1我喜欢这个比我使用的要好得多:git submodule foreach git submodule init ...然后git submodule update --recursive – 2012-03-27 14:19:18

+0

不幸的是,这并不适用于我。没有错误,没有消息,什么都没有。 – 2014-02-10 15:50:43

49

Sridhar下面的评论,从Git1.6.5 +,git clone --recursive现在是正式的替代,在所描述:

inamiy正确points outgit submodule update --init --recursive命令,在commit b13fd5c中引入,再次在git1.6.5中,由Johan Herland (jherland)引入。

而且IceFire增加in the comments

如果你想只检出一个子模块的子模块,那么
git submodule update --init <submoduleName>是要走的路。


(旧的原来的答案)

按照manual page

git submodule update --recursive 

应该更新任何嵌套子模块。但init部分可能不是递归的。

根据您的Git版本,你可以回落到一个更“脚本”的方法,这篇文章Recursively Updating Git Submodules它允许递归的初始化和更新:

#!/usr/bin/perl 

use strict; 
use Cwd; 

init_and_update(); 

exit; 

sub init_and_update 
{ 
    my $start_path = cwd(); 

    my %paths; 
    my $updated; 

    do 
    { 
     my $data = `find . -name '.gitmodules'`; 
     chomp($data); 

     $data =~ s/\/\.gitmodules//g; 

     foreach my $path (split(/\n/, $data)) 
     { 
      $paths{$path} = '' if($paths{$path} eq ''); 
     } 

     $updated = 0; 

     foreach my $path (sort keys %paths) 
     { 
      if($paths{$path} eq '') 
      { 
       chdir($path); 
       `git submodule init 2>&1`; 
       `git submodule update 2>&1`; 
       chdir($start_path); 

       if($ARGV[0] eq '--remove-gitmodules') 
       { 
        unlink("$path/.gitmodules"); 
       } 

       $paths{$path} = 1; 

       $updated++; 
      } 
     } 
    } while($updated); 
} 
+1

“git clone --recursive”是否足够? – 2011-01-12 21:13:06

+0

@Sridhar:这是克隆,如http://stackoverflow.com/questions/3796927/git-clone-submodule和http://stackoverflow.com/questions/4251940/retrospectively-add-recursive-to中提到的-a-git-repo/4261001#4261001,来自Git1.6.5及更高版本。我编辑了我的答案以反映这一点。 – VonC 2011-01-12 21:19:24

+0

注意:如果您只想签出一个子模块的一个子模块,那么'git submodule update --init '是要走的路;我在搜索这个答案时来到了这里 – IceFire 2017-08-30 15:09:41