Easy way to pull latest of all git submodules

2018-12-31 14:24发布

We're using git submodules to manage a couple of large projects that have dependencies on many other libraries we've developed. Each library is a separate repo brought into the dependent project as a submodule. During development, we often want to just go grab the latest version of every dependent submodule.

Does git have a built in command to do this? If not, how about a Windows batch file or similar that can do it?

18条回答
明月照影归
2楼-- · 2018-12-31 15:14

For git 1.8.2 or above the option --remote was added to support updating to latest tips of remote branches:

git submodule update --recursive --remote

This has the added benefit of respecting any "non default" branches specified in the .gitmodules or .git/config files (if you happen to have any, default is origin/master, in which case some of the other answers here would work as well).

For git 1.7.3 or above you can use (but the below gotchas around what update does still apply):

git submodule update --recursive

or:

git pull --recurse-submodules

if you want to pull your submodules to latest commits intead of what the repo points to.

Note: If that's the first time you checkout a repo you need to use --init first:

git submodule update --init --recursive

For older, git 1.6.1 or above you can use something similar to (modified to suit):

git submodule foreach git pull origin master

See git-submodule(1) for details

查看更多
步步皆殇っ
3楼-- · 2018-12-31 15:17

Here is the command-line to pull from all of your git repositories whether they're or not submodules:

ROOT=$(git rev-parse --show-toplevel 2> /dev/null)
find "$ROOT" -name .git -type d -execdir git pull -v ';'

If you running it in your top git repository, you can replace "$ROOT" into ..

查看更多
弹指情弦暗扣
4楼-- · 2018-12-31 15:19

Henrik is on the right track. The 'foreach' command can execute any arbitrary shell script. Two options to pull the very latest might be,

git submodule foreach git pull origin master

and,

git submodule foreach /path/to/some/cool/script.sh

That will iterate through all initialized submodules and run the given commands.

查看更多
柔情千种
5楼-- · 2018-12-31 15:19

Edit:

In the comments was pointed out (by philfreo ) that the latest version is required. If there is any nested submodules that need to be in their latest version :

git submodule foreach --recursive git pull

-----Outdated comment below-----

Isn't this the official way to do it ?

git submodule update --init

I use it every time. No problems so far.

Edit:

I just found that you can use:

git submodule foreach --recursive git submodule update --init 

Which will also recursively pull all of the submodules, i.e. dependancies.

查看更多
弹指情弦暗扣
6楼-- · 2018-12-31 15:22

The above answers are good, however we were using git-hooks to make this easier but it turns out that in git 2.14, you can set git config submodule.recurse to true to enable submodules to to updated when you pull to your git repository.

This will have the side effect of pushing all submodules change you have if they are on branches however, but if you have need of that behaviour already this could do the job.

Can be done by using:

git config submodule.recurse true
查看更多
若你有天会懂
7楼-- · 2018-12-31 15:22

All you need to do now is a simple git checkout

Just make sure to enable it via this global config: git config --global submodule.recurse true

查看更多
登录 后发表回答