How to change a git submodule URL

March 14, 2012 | 2 Minute Read

Let me save you some headache today: if you’re like me, you really do like keeping your git repo neat and clean using submodules for third party libraries. (Especially if the language you’re using doesn’t have something like CPAN or rubygem)

Everything runs perfect untill you have to change one submodule URL.

For istance: your submodule is a github repo and you can’t wait for your pull request to been accepted, so you press the fork button and commit the patch in your private repo. Now what?

Let ‘yourproject’ be your git working directory, ‘supercoolib’ the submodule and ‘yourusername’ your github username. Open with an editor of your choice (in my case emacs) the .gitmodules file (usually is located at the root of the repo):

1
2
$ cd yourproject
$ emacs .gitmodule

You should see something like this:

[submodule “supercoolib”]
path = lib/supercoolib
url = https://github.com/anotherusername/supercoolib.git

Change the url of the repo like that:

[submodule “supercoolib”]
path = lib/supercoolib
url = https://github.com/yourusername/supercoolib.git

Now tell git to update his internal reference:

1
$ git submodule sync

And then init(if you didn’t already) and update the submodules:

1
2
$ git submodule init
$ git submodule update

Don’t forget to checkout the branch inside the submodule (remember? submodules have a detached HEAD):

1
2
$ cd lib/supercoolib
$ git checkout master

Check if everything is working and finally commit your changes ;)