phi*_*reo 174 git git-submodules
有没有办法自动拥有git submodule update
(或最好随时git submodule update --init
调用git pull
?
寻找一个git配置设置,或者一个git别名来帮助解决这个问题.
Kan*_*ane 132
从git 2.14开始,您可以设置submodule.recurse
为true以启用所需的行为.
您可以通过运行来全局执行此操作:
git config --global submodule.recurse true
Run Code Online (Sandbox Code Playgroud)
Lil*_*ard 111
git config --global alias.pullall '!git pull && git submodule update --init --recursive'
如果你想将参数传递给git pull,那么请改用:
git config --global alias.pullall '!f(){ git pull "$@" && git submodule update --init --recursive; }; f'
Run Code Online (Sandbox Code Playgroud)
Chr*_*ers 42
从Git 1.7.5开始,它应该像你想要的那样自动更新子模块.
[编辑:每评论:新的1.7.5行为是自动获取最新提交的子模块,但不以更新他们(在git submodule update
意义上的).所以这个答案中的信息与背景相关,但本身并不是一个完整的答案.您仍然需要一个别名来在一个命令中提取和更新子模块.]
默认行为"按需"是每当您获取更新子模块提交的提交时更新子模块,并且此提交尚未位于您的本地克隆中.
您也可以在每次获取或从不更新时更新(我假设的1.7.5之前的行为).
用于更改此行为的config选项是fetch.recurseSubmodules
.
此选项可以设置为布尔值或
on-demand
.
将其设置为布尔值会更改行为,fetch
并pull
在设置为true时无条件地递归到子模块,或者在设置为false时根本不递归.当设置为
on-demand
(默认值),fetch
并pull
在其上层项目检索提交,更新子模块的引用将只迭代到一个人口稠密的子模块.
看到:
欲获得更多信息.
git fetch --recurse-submodules[=yes|on-demand|no]
Run Code Online (Sandbox Code Playgroud)
tal*_*nat 29
我很惊讶没有人提到使用git hooks来做到这一点!
只需添加文件命名post-checkout
和post-merge
你.git/hooks
有关的信息库的目录,并把下列它们:
#!/bin/sh
git submodule update --init --recursive
Run Code Online (Sandbox Code Playgroud)
由于您明确要求别名,假设您希望为多个存储库提供此别名,您可以创建一个别名,将这些别名添加到存储库中.git/hooks
.
您可以为自动处理子模块更新的git命令创建别名.将以下内容添加到.bashrc中
# make git submodules usable
# This overwrites the 'git' command with modifications where necessary, and
# calls the original otherwise
git() {
if [[ $@ == clone* ]]; then
gitargs=$(echo "$@" | cut -c6-)
command git clone --recursive $gitargs
elif [[ $@ == pull* ]]; then
command git "$@" && git submodule update --init --recursive
elif [[ $@ == checkout* ]]; then
command git "$@" && git submodule update --init --recursive
else
command git "$@"
fi
}
Run Code Online (Sandbox Code Playgroud)
正如其他人所提到的,您可以使用以下方法轻松设置:
git config --global submodule.recurse true
Run Code Online (Sandbox Code Playgroud)
但是,如果您像我一样,并且.gitconfig
设置更复杂(我的主~/.gitconfig
文件用于include
加载其他.gitconfig
文件),并且您永远都不记得如何在命令行git
配置格式和.gitconfig
格式之间进行转换,那么这是添加方法到您的任何.gitconfig
文件:
[submodule]
recurse = true
Run Code Online (Sandbox Code Playgroud)