有没有办法让 git pull 自动更新子模块?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4611512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to make git pull automatically update submodules?
提问by philfreo
Is there a way to automatically have git submodule update
(or preferably git submodule update --init
called whenever git pull
is done?
有没有办法自动拥有git submodule update
(或者最好git submodule update --init
在git pull
完成时调用?
Looking for a git config setting, or a git alias to help with this.
寻找 git config 设置或 git 别名来帮助解决这个问题。
采纳答案by Kane
As of Git 2.14, you can use git pull --recurse-submodules
(and alias it to whatever you like).
从Git 2.14 开始,您可以使用git pull --recurse-submodules
(并将其别名为您喜欢的任何内容)。
As of Git 2.15, you could set submodule.recurse
to true to enable the desired behaviour.
从Git 2.15 开始,您可以设置submodule.recurse
为 true 以启用所需的行为。
You can do this globally by running:
您可以通过运行来全局执行此操作:
git config --global submodule.recurse true
回答by Lily Ballard
git config --global alias.pullall '!git pull && git submodule update --init --recursive'
git config --global alias.pullall '!git pull && git submodule update --init --recursive'
If you want arguments to be passed to git pull, then use this instead:
如果您希望将参数传递给 git pull,请改用以下命令:
git config --global alias.pullall '!f(){ git pull "$@" && git submodule update --init --recursive; }; f'
回答by Christopher Rogers
Starting with Git 1.7.5 it should update submodules automatically by default like you want it to.
从 Git 1.7.5 开始,它应该默认自动更新子模块,就像你想要的那样。
[EDIT: per comments: the new 1.7.5 behaviour is to automatically fetchthe latest commits for submodules, but notto updatethem (in the git submodule update
sense). So the information in this answer is relevant as background, but is not a complete answer by itself. You still need an alias to pull and update submodules in one command.]
[编辑:每评论:新的1.7.5行为是自动获取最新提交的子模块,但不以更新他们(在git submodule update
意义上的)。因此,此答案中的信息与背景相关,但本身并不是完整的答案。您仍然需要一个别名来在一个命令中拉取和更新子模块。]
The default behavior, "on-demand", is to update submodules whenever you fetch a commit that updates the submodule commit, and this commit isn't already located in your local clone.
You can also have it updated on every fetch or never (pre-1.7.5 behavior I assume).
The config option to change this behavior is fetch.recurseSubmodules
.
默认行为“按需”是在您获取更新子模块提交的提交时更新子模块,并且此提交尚未位于您的本地克隆中。
您还可以在每次获取或从不更新它(我假设 1.7.5 之前的行为)。
更改此行为的配置选项是fetch.recurseSubmodules
.
This option can be either set to a boolean value or to
on-demand
.
Setting it to a boolean changes the behavior offetch
andpull
to unconditionally recurse into submodules when set to true or to not recurse at all when set to false.When set to
on-demand
(the default value),fetch
andpull
will only recurse into a populated submodule when its superproject retrieves a commit that updates the submodule's reference.
此选项可以设置为布尔值或
on-demand
.
将其设置为布尔值会更改 和 的行为,fetch
并pull
在设置为 true 时无条件地递归到子模块中,或者在设置为 false 时根本不递归。当设置为
on-demand
(默认值)时,fetch
并且pull
只会在其超级项目检索更新子模块引用的提交时递归到填充的子模块中。
See:
看:
git config
man page (1.7.5)(or latestgit config
man page)git fetch
man page (1.7.5)(or latest git fetch man page)
for more information.
想要查询更多的信息。
git fetch --recurse-submodules[=yes|on-demand|no]
回答by taleinat
I'm surprised nobody mentioned using git hooks to do this!
我很惊讶没有人提到使用 git hooks 来做到这一点!
Just add files named post-checkout
and post-merge
to your .git/hooks
directory of the relevant repositories, and put the following into each of them:
只需将名为post-checkout
和 的文件添加post-merge
到.git/hooks
相关存储库的目录中,并将以下内容放入每个文件中:
#!/bin/sh
git submodule update --init --recursive
Since you specfically asked for an alias, assuming you want to have this for many repositories, you can create an alias which adds these to a repository's .git/hooks
for you.
由于您特别要求提供别名,假设您想为许多存储库使用这个别名,您可以创建一个别名,.git/hooks
为您将这些添加到存储库中。
回答by Cascabel
An alias, as suggested by Kevin Ballard, is a perfectly good solution. Just to toss another option out there, you could also use a post-merge hook which simply runs git submodule update [--init]
.
正如 Kevin Ballard 所建议的,别名是一个非常好的解决方案。只是为了抛出另一个选项,您还可以使用简单地运行git submodule update [--init]
.
回答by Branden Ghena
You can create an alias for the git command that automatically handles submodule updating. Add the following to your .bashrc
您可以为自动处理子模块更新的 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
}
回答by JacobEvelyn
As others have mentioned, you can easily set this with:
正如其他人所提到的,您可以通过以下方式轻松设置:
git config --global submodule.recurse true
However, if you're like me and have a more complex .gitconfig
setup (my main ~/.gitconfig
file uses include
to load in other .gitconfig
files), and you can never remember how to convert between the command-line git
config format and the .gitconfig
format, here's how to add it to any of your .gitconfig
files:
但是,如果您像我一样拥有更复杂的.gitconfig
设置(我的主~/.gitconfig
文件用于include
加载其他.gitconfig
文件),并且您永远不会记得如何在命令行git
配置格式和.gitconfig
格式之间进行转换,那么这里是如何添加它到您的任何.gitconfig
文件:
[submodule]
recurse = true
回答by Sauli Kiviranta
Only way how I was able to get the submodules and nested submodules to update:
我如何能够更新子模块和嵌套子模块的唯一方法:
git submodule update --remote --merge --recursive; git submodule foreach --recursive "(git add .; git commit -m 'SubmoduleSync'; git push; git pull;);" git add .; git commit -m 'SubmodulesSynced'; git push; git pull;
I was struggling to create the alias through terminal due to the brackets so I had to manually add this to .gitconfig for global:
由于括号,我正在努力通过终端创建别名,因此我不得不手动将其添加到 .gitconfig 全局:
[alias] supdate = "!git submodule update --remote --merge --recursive; git submodule foreach --recursive '(git add .; git commit -m 'SubmoduleSync'; git push; git pull;);' git add .; git commit -m 'SubmodulesSynced'; git push; git pull;"
Any suggestions for how to run the commands or the alias automatically?
关于如何自动运行命令或别名的任何建议?