如何将现有的嵌套存储库(已在子目录中签出)作为子模块添加到父 Git 存储库?

2024-03-25

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

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

那么:在初始“git add”之后如何继续。在父工作目录中,其中存在具有独立签出的嵌套 git 存储库的子目录(为了获得正确的子模块)?

一个例子:

[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/sub里面的 git 仓库super-existing-sub.

我跑进去后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

$ 

but git submodule有一些问题:

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

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


我尝试按照答案(由维克多和我组成)中建议的方式进行操作,即git submodule add URL subdir,但不幸的是,这打破了:

$ git submodule status
No submodule mapping found in .gitmodules for path 'wp-content/themes/liquorice'
$ git submodule add [email protected] /cdn-cgi/l/email-protection:/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'
$ 

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

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 存储库的文件夹,并且您希望父存储库将它们全部作为子模块:

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. 此单行代码为每个可能的子模块打印该命令。试运行一下以查看文件夹中有哪些内容.git/config文件包含url =。请注意,它假设第一次出现url是远程的 - 请确保对结果进行健全性检查。

线人再次解释道:

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 g[email protected] /cdn-cgi/l/email-protection:another_repo.git path/to/another_repo
(etc.)

2.然后,真正执行:去除echo在命令的末尾,所以最后一部分从

xargs -n 2 echo git submodule add

to

xargs -n 2 git submodule add

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

3.添加到git。您现在可以运行git status看看你刚刚做了什么。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将现有的嵌套存储库(已在子目录中签出)作为子模块添加到父 Git 存储库? 的相关文章

随机推荐