我有一个文件夹,是一个git repo。它包含一些文件和.gitmodules文件。现在,当我执行git init,然后执行git submodule init时,后面的命令输出为nothing。如何帮助git在不再次手动运行git submodule add的情况下查看.gitmodules文件中定义的子模块?
更新:这是我的.gitmodules文件:
[submodule "vim-pathogen"]
path = vim-pathogen
url = git://github.com/tpope/vim-pathogen.git
[submodule "bundle/python-mode"]
path = bundle/python-mode
url = git://github.com/klen/python-mode.git
[submodule "bundle/vim-fugitive"]
path = bundle/vim-fugitive
url = git://github.com/tpope/vim-fugitive.git
[submodule "bundle/ctrlp.vim"]
path = bundle/ctrlp.vim
url = git://github.com/kien/ctrlp.vim.git
[submodule "bundle/vim-tomorrow-theme"]
path = bundle/vim-tomorrow-theme
url = git://github.com/chriskempson/vim-tomorrow-theme.git下面是这个目录的清单:
drwxr-xr-x 4 evgeniuz 100 4096 июня 29 12:06 .
drwx------ 60 evgeniuz 100 4096 июня 29 11:43 ..
drwxr-xr-x 2 evgeniuz 100 4096 июня 29 10:03 autoload
drwxr-xr-x 7 evgeniuz 100 4096 июня 29 12:13 .git
-rw-r--r-- 1 evgeniuz 100 542 июня 29 11:45 .gitmodules
-rw-r--r-- 1 evgeniuz 100 243 июня 29 11:18 .vimrc所以,它绝对是顶级的。不更改git目录,仅执行git init。
发布于 2012-06-29 16:54:33
git submodule init只考虑已经在索引中的子模块(即"staged")进行初始化。我将编写一个简短的脚本来解析.gitmodules,并为每个url和path对运行:
git submodule add <url> <path>例如,您可以使用以下脚本:
#!/bin/sh
set -e
git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
while read path_key path
do
url_key=$(echo $path_key | sed 's/\.path/.url/')
url=$(git config -f .gitmodules --get "$url_key")
git submodule add $url $path
done这基于git-submodule.sh script本身解析.gitmodules文件的方式。
发布于 2013-03-09 04:06:50
根据@Mark Longair的回答,我编写了一个bash脚本来自动执行以下过程的步骤2和3:
#!/bin/bash
set -e
rm -rf .git
git init
git config -f .gitmodules --get-regexp '^submodule\..*\.path$' > tempfile
while read -u 3 path_key path
do
url_key=$(echo $path_key | sed 's/\.path/.url/')
url=$(git config -f .gitmodules --get "$url_key")
read -p "Are you sure you want to delete $path and re-initialize as a new submodule? " yn
case $yn in
[Yy]* ) rm -rf $path; git submodule add $url $path; echo "$path has been initialized";;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done 3<tempfile
rm tempfile注意:子模块将在其主分支的顶端签出,而不是与样板代码库相同的提交,因此您需要手动执行此操作。
将git config的输出通过管道传输到read循环会导致输入提示符出现问题,因此它会将其输出到一个临时文件。对我的第一个bash脚本的任何改进都将非常受欢迎:)
非常感谢Mark、https://stackoverflow.com/a/226724/193494、bash: nested interactive read within a loop that's also using read和tnettenba @ chat.freenode.net帮助我实现了这个解决方案!
发布于 2018-11-13 04:30:15
扩展卓越的@Mark Longair的答案,添加关于分支和存储库名称的子模块。
#!/bin/sh
set -e
git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
while read path_key path
do
name=$(echo $path_key | sed 's/\submodule\.\(.*\)\.path/\1/')
url_key=$(echo $path_key | sed 's/\.path/.url/')
branch_key=$(echo $path_key | sed 's/\.path/.branch/')
url=$(git config -f .gitmodules --get "$url_key")
branch=$(git config -f .gitmodules --get "$branch_key" || echo "master")
git submodule add -b $branch --name $name $url $path || continue
donehttps://stackoverflow.com/questions/11258737
复制相似问题