Changing Git submodule remote repository

A Git submodule allows you to add other repositories inside your project source tree, they’re handful for application modules or plugins, so you can develop reusable plugins independently of your app. When you add submodules, it is created a file called .gitmodules, in your source tree root, where are stored all submodule information (remote repository URL and it’s path on the source tree).
During the development of a project, the source of a submodule may change for any reason (the server URL changed, the repository’s name or path changed, etc…), and you’ll have to update those changes in your apps, otherwise you won’t be able to push or pull on those submodules. This task is not that easy, because Git apart from storing submodule information in the .gitmodules file, it stores the same information in the repository configuration file (.git/config), and every submodule’s repository remote URL. But fortunately, there’s an easier way to do that using the ‘git submodule sync’ command, all you have to do is:

1) Edit the .gitmodules file, and change the URL for the submodules which changed.

2) In your source tree’s root run:

user@host:/path/to/repo$ git submodule sync

3) Then run git init to update the project’s repository configuration with the new URLs:

user@host:/path/to/repo$ git submodule init

And it’s done, now you can continue pushing and pulling your submodules with no problems :)

    • Cyber1000
    • November 16th, 2011

    Hi! Nice post, but what if you change the serverurl, do the “git submodule sync” as you said and then want to checkout an older version of your project (where .gitmodules is different, pointing to your old serverurl). With git submodules update you would get errors, if the old serveraccess doesn’t work anymore, wouldn’t you?
    My problem is I started with git as a normal ssh connection and now moved to managing with gitolite (where all the serverlinks would change) …

  1. Thanks! I had quite a few submodules and it turned out one of them had moved (again). Finally figured it out, thanks to this post! :-)

  1. June 14th, 2018
  2. October 2nd, 2018
Comments are closed.
%d bloggers like this: