Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git submodule update vs git submodule sync

The git documentation doesn't make it at all clear what the difference is between a git submodule update and a git submodule sync is. I'm also not finding any help out on the web. Can someone help me out with what the difference is here?

   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. 

-

   sync        Synchronizes submodules' remote URL configuration setting to the value        specified in .gitmodules. It will only affect those submodules which        already have a URL entry in .git/config (that is the case when they are        initialized or freshly added). This is useful when submodule URLs        change upstream and you need to update your local repositories        accordingly. 

For reference, I'm using the git client version 2.11.0

like image 602
Benjamin Leinweber Avatar asked Aug 14 '17 16:08

Benjamin Leinweber


People also ask

What is git submodule sync?

git submodule sync synchronizes all submodules while git submodule sync -- A synchronizes submodule "A" only. If --recursive is specified, this command will recurse into the registered submodules, and sync any nested submodules within.

What does git submodule update do?

The git submodule update command sets the Git repository of the submodule to that particular commit. The submodule repository tracks its own content which is nested into the main repository. The main repository refers to a commit of the nested submodule repository.

What is git submodule update -- init -- recursive?

git submodule update --init --recursive --remote - updates all submodules recursively along their tracking branches. Without the --remote , it'll reset the submodule working directories to the "right" commit for the parent.


1 Answers

update is basically doing git pull in each submodule (except without a branch, since the main repo specifies a commit directly).

The tricky one is sync. Imagine you clone a project with submodules, then later the upstream project changes one of the submodules to point to a different URL.

Your local copy of the submodule will still point to the old URL, since git never allows remote repositories to force a change to local configuration. You need to run git submodule sync to apply the remote repo's configuration to your local submodule repos.

Note also that, if you are making changes to the submodules, you might want the URLs to mismatch even if the upstream never changed them ... but using multiple remote URLs is probably a better idea for that case.

like image 163
o11c Avatar answered Sep 24 '22 22:09

o11c