基于 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
看看你刚刚做了什么。