【问题标题】:How to add an existing nested repo (already checked out in a subdir) to a parent Git repo as a submodule?如何将现有的嵌套存储库(已在子目录中签出)添加到父 Git 存储库作为子模块?
【发布时间】:2015-04-09 08:46:13
【问题描述】:

如果我从我的工作(父)目录创建初始提交,但存在具有独立签出 git repos 的子目录,会发生什么情况?

我只是做了git add .,但这让我遇到了一个奇怪的情况,即嵌套 Git 存储库的子目录未注册为父存储库的子模块。

所以:如何在初始“git add”之后继续。在父工作目录中,其中存在具有独立签出的嵌套 git repos 的子目录(以获取正确的子模块)?

一个例子:

[imz@z super-existing-sub]$ ls 
c  sub
[imz@z super-existing-sub]$ ls -a sub/
.  ..  a  .git
[imz@z super-existing-sub]$ 

所以,super-existing-sub 内部已经有一个预先存在的 super-existing-sub/sub git repo。

在我跑完super-existing-sub:

$ git init
$ git add .

如何才能将预先存在的 Git 存储库正确注册为子模块?

现在,Git 以某种方式追踪到了它:

$ git status 
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   c
    new file:   sub

$ 

但是git submodule有一些问题:

$ git submodule status 
No submodule mapping found in .gitmodules for path 'sub'
$ 

如何将其转换为正确的子模块?


我尝试按照答案中建议的方式(由 Victor 和我组成),即git submodule add URL subdir,但不幸的是,它中断了:

$ git submodule status
No submodule mapping found in .gitmodules for path 'wp-content/themes/liquorice'
$ git submodule add git@github.com:/nudgeme/Liquorice.git ./wp-content/themes/liquorice
'wp-content/themes/liquorice' already exists in the index
/sshx:kosmoplus:/home/kosmoplus/kosmoplus.ru.old $ git submodule status
No submodule mapping found in .gitmodules for path 'wp-content/themes/liquorice'
$ 

【问题讨论】:

标签: git git-submodules git-add git-init


【解决方案1】:

基于 Victor Johnson 的回答,这里有一个 bash oneliner,用于一次添加一整套子模块。

find . -name config | grep git/config | grep -v ProjectToExclude | grep -v AnotherProjectToExclude | xargs -n 1 -I % sh -c 'cat % | grep -m 1 url | sed "s/url = //g"; echo % | sed "s/.git\/config//g";' | xargs -n 2 echo git submodule add

详细解释

假设您有一个包含许多 git repos 的文件夹,并且您希望父 repo 将它们全部作为子模块:

path/to/one_repo
path/to/one_repo/.git/config
path/to/another_repo
path/to/another_repo/.git/config
(etc.)

您想为其中的每一个运行git submodule add ./path/to/submodule/

1.此单行程序会为每个可能的子模块打印该命令。 将其作为空运行运行,以查看哪些文件夹具有包含 url =.git/config 文件。请注意,它假定url 的第一次出现是远程的 - 确保您检查结果的完整性。

又是 oneliner,解释:

find . -name config |                            # find all config files
    grep git/config |                            # include only those that have "git/config" in their path
    grep -v ProjectToExclude |                   # exclude config files with ProjectToExclude in their path
    grep -v AnotherProjectToExclude |            # ditto
    xargs -n 1 -I % sh -c '                      # one line at a time, execute the following commands, substituting the path for `%`
        cat % |                                  # print the config file contents
            grep -m 1 url |                      # take the first line that contains `url`
            sed "s/url = //g";                   # remove the "url = " part
        echo % |                                 # print the path to the config file
            sed "s/.git\/config//g";             # remove '.git/config' to keep only the project path
    ' |                                          # summary: print the remote url on every 2nd line; the path on every 2nd line 
    xargs -n 2 echo git submodule add            # take two lines at a time, print them with the command

请注意,您可以将任意数量的 grep -v ProjectToExclude 添加到管道以排除这些文件夹 - 再次运行 oneliner 以查看要执行的新命令列表,并注意 ProjectToExclude 不再存在。

这将打印出来:

git submodule add https://github.com/username/one_repo.git path/to/one_repo
git submodule add git@otherdomain.com:another_repo.git path/to/another_repo
(etc.)

2。然后,实际执行: 去掉命令末尾的echo,所以最后一部分变成了

xargs -n 2 echo git submodule add

xargs -n 2 git submodule add

(我们实际上将执行它,而不是回显命令。)

3.将其添加到 git。 您现在可以运行 git status 以查看您刚刚完成的操作。

【讨论】:

  • 非常感谢——这个单行字,加上解释,帮了大忙!
【解决方案2】:

(我一直使用git submodule init 让 git 识别它们,然后使用git submodule update 将子模块实际克隆到工作目录中。但这不是被询问的情况。)

要从父目录创建新的 git repo,您​​需要在父目录上运行 git init,然后运行 ​​git submodule add ./path/to/submodule/

注意:子模块的路径必须是绝对路径,所以你应该在路径前加上./

或者,如果你想有一个好的外部 URL 作为子模块的 URL,你应该先在 ./path/to/submodule/.git/config 中查找源 URL,然后你应该可以

git submodule add URL ./path/to/submodule/

在实践中还没有尝试过,但是 git-submodule 的手册页说:

如果&lt;path&gt; 确实存在并且已经是一个有效的 Git 存储库,那么它会被添加到变更集中而不进行克隆。提供第二种形式是为了便于从头开始创建新的子模块,并假定用户稍后会将子模块推送到给定的 URL。

【讨论】:

  • 如果带有 Git 存储库的子目录已经存在怎么办? (所以我不必克隆子模块,因为它就在那里。然后我想用子模块使封闭目录也成为 Git 存储库。)
  • 那么,如果子目录中已经有一个签出的 Git 存储库并且 .gitmodules 中还没有条目,那么 git submodule init 将不起作用,是吗?
  • 至于第二个建议(git submodule add):这似乎是我从手册页中遗漏的东西。确实:“如果 确实存在并且已经是一个有效的 Git 存储库,那么它会被添加到变更集中而无需克隆。提供第二种形式是为了方便从头开始创建新的子模块,并假定用户稍后将推送子模块到给定的 URL。”对我来说,剩下的唯一部分可能是获取子回购的“起源”并将其作为git submodule add 的第一个参数。之后,我的子模块一定是正确的。
  • 对于你的第一个问题:不,如果子模块已经签出,我认为git submodule init 不会做任何事情,但你仍然需要运行git submodule add 来告诉 git 它存在。对于您的第二条评论:如果您指的是添加一个新的子模块(从源)到一个 repo,那么您只需要在您想要放置子模块的目录中运行git submodule add http://url/to/submodule
  • 我的意思是:当有一个现有的带有 Git repo 的子目录时,我应该查看该子目录 () 中的原始 URL,然后可能运行 git submodule add URL subdir 来做我想做的事:添加一个现有的仓库(已经签出)到一个父 Git 仓库作为子模块。
【解决方案3】:

请参阅 TL;DR 在最后。

让我们仔细阅读git submodule &lt;URL&gt; &lt;path&gt; 的手册页,注意以下注释:

如果&lt;path&gt; 确实存在并且已经是一个有效的 Git 存储库,那么它将被添加到变更集中而不进行克隆。提供第二种形式是为了便于从头开始创建新的子模块,并假定用户稍后会将子模块推送到给定的 URL。

让我们尝试在像我们这样的情况下使用它(在您将父目录递归添加到新的父 repo 之后,而已经存在一个嵌套的 Git repo)。

这个想法是,首先,在./path/to/submodule/.git/config 中查找原始 URL,以便为有关子模块的元信息获取一个不错的真实外部 URL,其次,调用 git submodule &lt;URL&gt; &lt;path&gt;(希望它可以实现我们想要的,根据手册页)。

让我们试试吧:

$ git submodule status
No submodule mapping found in .gitmodules for path 'wp-content/themes/liquorice'
$ git submodule add git@github.com:/nudgeme/Liquorice.git ./wp-content/themes/liquorice
'wp-content/themes/liquorice' already exists in the index
/sshx:kosmoplus:/home/kosmoplus/kosmoplus.ru.old $ git submodule status
No submodule mapping found in .gitmodules for path 'wp-content/themes/liquorice'
$ 

不幸的是,它坏了。

好吧,让我们想想我们如何解决“索引中已经存在”而不是我们想要的东西的问题......

...我们可以简单地将其从索引中删除!这成功了:

$ git submodule status
No submodule mapping found in .gitmodules for path 'wp-content/themes/liquorice'
$ git rm --cached ./wp-content/themes/liquorice
rm 'wp-content/themes/liquorice'
$ git submodule add git@github.com:/nudgeme/Liquorice.git ./wp-content/themes/liquorice
Adding existing repo at 'wp-content/themes/liquorice' to the index
$ git submodule status
 9733c0ab3e4207352e3f51d612f2a1c9c4a0b63a wp-content/themes/liquorice (liquorice2.1-1-g9733c0a)
$ 

TL;DR

  • 在子目录中查找源 URL;
  • git rm --cached subdir
  • git submodule add URL subdir

这甚至可以制作一个简单的脚本来自动将现有的嵌套存储库作为子模块添加到父级中的任务,但我看不出增加东西的意义。在这个级别,只需学习基本的 Git 命令,并使用和组合它们!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-03
    • 2017-06-29
    • 1970-01-01
    • 2016-07-23
    • 2013-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多