Update Git submodule to latest commit on origin

后端 未结 14 1417
清歌不尽
清歌不尽 2020-11-27 09:02

I have a project with a Git submodule. It is from an ssh://... URL, and is on commit A. Commit B has been pushed to that URL, and I want the submodule to retrieve the commit

相关标签:
14条回答
  • 2020-11-27 09:07

    Plain and simple, to fetch the submodules:

    git submodule update --init --recursive
    

    And now proceed updating them to the latest master branch (for example):

    git submodule foreach git pull origin master
    
    0 讨论(0)
  • 2020-11-27 09:09

    @Jason is correct in a way but not entirely.

    update

    Update the registered submodules, i.e. clone missing submodules and checkout the commit specified in the index of the containing repository. This will make the submodules HEAD be detached unless --rebase or --merge is specified or the key submodule.$name.update is set to rebase or merge.

    So, git submodule update does checkout, but it is to the commit in the index of the containing repository. It does not yet know of the new commit upstream at all. So go to your submodule, get the commit you want and commit the updated submodule state in the main repository and then do the git submodule update.

    0 讨论(0)
  • 2020-11-27 09:11

    In my case, I wanted git to update to the latest and at the same time re-populate any missing files.

    The following restored the missing files (thanks to --force which doesn't seem to have been mentioned here), but it didn't pull any new commits:

    git submodule update --init --recursive --force

    This did:

    git submodule update --recursive --remote --merge --force

    0 讨论(0)
  • 2020-11-27 09:15
    git pull --recurse-submodules
    

    This will pull all the latest commits.

    0 讨论(0)
  • 2020-11-27 09:16

    Your main project points to a particular commit that the submodule should be at. git submodule update tries to check out that commit in each submodule that has been initialized. The submodule is really an independent repository - just creating a new commit in the submodule and pushing that isn't enough. You also need to explicitly add the new version of the submodule in the main project.

    So, in your case, you should find the right commit in the submodule - let's assume that's the tip of master:

    cd mod
    git checkout master
    git pull origin master
    

    Now go back to the main project, stage the submodule and commit that:

    cd ..
    git add mod
    git commit -m "Updating the submodule 'mod' to the latest version"
    

    Now push your new version of the main project:

    git push origin master
    

    From this point on, if anyone else updates their main project, then git submodule update for them will update the submodule, assuming it's been initialized.

    0 讨论(0)
  • 2020-11-27 09:16

    If you don't know the host branch, make this:

    git submodule foreach git pull origin $(git rev-parse --abbrev-ref HEAD)
    

    It will get a branch of the main Git repository and then for each submodule will make a pull of the same branch.

    0 讨论(0)
提交回复
热议问题