如果我从我的工作(父)目录创建初始提交,但有子目录具有独立 checkout 的 git 存储库,会发生什么情况?
我只是执行了 git add .
但这让我陷入了一个奇怪的境地,因为带有嵌套 Git 存储库的子目录没有注册为父存储库的子模块。
因此:如何在初始“git add”之后继续进行。在父工作目录中有子目录,其中有独立 checkout 的嵌套 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
中已经有一个预先存在的 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'
$
最佳答案
基于 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 存储库的文件夹,并且您希望父存储库将它们全部作为子模块:
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。此 oneliner 为每个可能的子模块打印该命令。将其作为空运行运行以查看哪些文件夹具有包含 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
来查看您刚刚完成的操作。
关于git - 如何将现有的嵌套存储库(已在子目录中 check out )作为子模块添加到父 Git 存储库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28412699/