我们使用git子模块来管理一些依赖于我们开发的许多其他库的大型项目。每个库都是一个单独的repo,作为子模块引入到依赖项目中。在开发过程中,我们经常想要获取每个依赖子模块的最新版本。
如何获取所有git子模块的最新更改?
我们使用git子模块来管理一些依赖于我们开发的许多其他库的大型项目。每个库都是一个单独的repo,作为子模块引入到依赖项目中。在开发过程中,我们经常想要获取每个依赖子模块的最新版本。
如何获取所有git子模块的最新更改?
当前回答
注:这是从2009年开始的,当时可能不错,但现在有更好的选择。
我们使用这个。它叫git pup:
#!/bin/bash
# Exists to fully update the git repo that you are sitting in...
git pull && git submodule init && git submodule update && git submodule status
只需将其放在合适的bin目录(/usr/local/bin)中。如果在Windows上,您可能需要修改语法以使其正常工作:)
更新:
作为对原作者关于插入所有子模块的所有HEAD的评论的回应,这是一个很好的问题。
我很确定git内部没有这个命令。为了做到这一点,您需要确定HEAD对于子模块的真正含义。这可能很简单,比如说master是最新的分支,等等。。。
接下来,创建一个简单的脚本,执行以下操作:
检查git子模块状态以查找“已修改”的存储库。输出行的第一个字符表示这一点。如果子回购被修改,您可能不想继续。对于列出的每个回购,将其cd到其目录中,并运行gitcheckout-master和gitpull。检查错误。最后,我建议您向用户打印一个显示,以指示子模块的当前状态——也许会提示他们添加全部并提交?
我想指出的是,这种风格并不是git子模块的设计初衷。通常,你想说“LibraryX”的版本是“2.32”,在我告诉它“升级”之前,它会一直这样。
从某种意义上说,这就是您使用所描述的脚本所做的事情,但只是更加自动。需要小心!
更新2:
如果您使用的是windows平台,您可能需要考虑使用Python来实现脚本,因为它在这些方面非常有用。如果您使用的是unix/linux,那么我建议您使用bash脚本。
需要澄清吗?只需发表评论。
其他回答
我通过修改上面gahooa的回答做到了这一点:
将其与git〔alias〕集成。。。
如果您的父项目在.gitmodules中有类似的内容:
[submodule "opt/submodules/solarized"]
path = opt/submodules/solarized
url = git@github.com:altercation/solarized.git
[submodule "opt/submodules/intellij-colors-solarized"]
path = opt/submodules/intellij-colors-solarized
url = git@github.com:jkaving/intellij-colors-solarized.git
在.gitconfig中添加类似的内容
[alias]
updatesubs = "!sh -c \"git submodule init && git submodule update && git submodule status\" "
然后,要更新子模块,请运行:
git updatesubs
我在环境设置repo中有一个示例。
我编写了这个简单的shell脚本,对我来说很好。
#!/bin/bash
#git init
git init
#git clone repo including submodules
git clone --recurse-submodules https://github.com/username/project.git
#change directory -repo
cd project
#update the remote ie tag/commits
git submodule update --remote
#add commit
git commit -a -m "commit in submodule"
#git push
git push -u origin
上面的答案很好,不过我们使用了git钩子来简化这一过程,但事实证明,在git2.14中,您可以将git-config子模块.recure设置为true,以便在您拉到git存储库时更新子模块。
如果所有子模块都在分支上,这会产生一个副作用,即推动所有子模块的更改,但如果您已经需要这种行为,这可以完成任务。
可通过以下方式完成:
git config submodule.recurse true
在init上运行以下命令:
git submodule update --init --recursive
在gitrepo目录中,最适合我。
这将拉动所有最新的子模块。
解释
git - the base command to perform any git command
submodule - Inspects, updates and manages submodules.
update - Update the registered submodules to match what the superproject
expects by cloning missing submodules and updating the working tree of the
submodules. The "updating" can be done in several ways depending on command
line options and the value of submodule.<name>.update configuration variable.
--init without the explicit init step if you do not intend to customize
any submodule locations.
--recursive is specified, this command will recurse into the registered
submodules, and update any nested submodules within.
之后,您可以运行:
git submodule update --recursive
在gitrepo目录中,最适合我。
这将拉动所有最新的子模块。
我经常使用这个命令,到目前为止,它仍然有效。
git pull
git submodule foreach --recursive git checkout master
git submodule foreach --recursive git pull
希望能快点。