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 14:58

On init running the following command:

git submodule update --init --recursive

from within the git repo directory, works best for me.

This will pull all latest including submodules.

Explained

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.

After this you can just run:

git submodule update --recursive

from within the git repo directory, works best for me.

This will pull all latest including submodules.

Explained

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.
            any submodule locations.
            --recursive is specified, this command will recurse into the registered
            submodules, and update any nested submodules within.
查看更多
美炸的是我
3楼-- · 2018-12-31 14:59

Look at http://lists.zerezo.com/git/msg674976.html which introduces a --track parameter

查看更多
深知你不懂我心
4楼-- · 2018-12-31 15:00

I did this by adapting gahooa's answer above:

Integrate it with a git [alias] ...

If your parent project has something like this in .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

Add something like this inside your .gitconfig

[alias]
    updatesubs = "!sh -c \"git submodule init && git submodule update && git submodule status\" "

Then to update your submodules, run:

git updatesubs

I have an example of it in my environment setup repo.

查看更多
初与友歌
5楼-- · 2018-12-31 15:02

I don't know since which version of git this is working, but that's what you're searching for:

git submodule update --recursive

I use it with git pull to update the root repository, too:

git pull && git submodule update --recursive
查看更多
栀子花@的思念
6楼-- · 2018-12-31 15:03

First time

Clone and Init Submodule

git clone git@github.com:speedovation/kiwi-resources.git resources
git submodule init

Rest

During development just pull and update submodule

git pull --recurse-submodules  && git submodule update --recursive

Update Git submodule to latest commit on origin

git submodule foreach git pull origin master

Preferred way should be below

git submodule update --remote --merge

note: last two commands have same behaviour

查看更多
与风俱净
7楼-- · 2018-12-31 15:05

If you need to pull stuff for submodules into your submodule repositories use

git pull --recurse-submodules

a feature git first learned in 1.7.3.

But this will not checkout proper commits(the ones your master repository points to) in submodules

To checkout proper commits in your submodules you should update them after pulling using

git submodule update --recursive --remote
查看更多
登录 后发表回答