How to change the remote repository for a git submodule? How to change the remote repository for a git submodule? git git

How to change the remote repository for a git submodule?


You should just be able to edit the .gitmodules file to update the URL and then run git submodule sync --recursive to reflect that change to the superproject and your working copy.

Then you need to go to the .git/modules/path_to_submodule dir and change its config file to update git path.

If repo history is different then you need to checkout new branch manually:

git submodule sync --recursivecd <submodule_dir> git fetchgit checkout origin/mastergit branch master -fgit checkout master


These commands will do the work on command prompt without altering any files on local repository

git config --file=.gitmodules submodule.Submod.url https://github.com/username/ABC.gitgit config --file=.gitmodules submodule.Submod.branch Developmentgit submodule syncgit submodule update --init --recursive --remote

Please look at the blog for screenshots: Changing GIT submodules URL/Branch to other URL/branch of same repository


With Git 2.25 (Q1 2020), you can modify it.
See "Git submodule url changed" and the new command

git submodule set-url [--] <path> <newurl>

Warning: Hi-Angel mentions in the comments (tested even with Git 2.31.1):

One should be careful with git submodule set-url because it has a bug:

If, inside your .gitmodules file, the path looks like this some-path, and then you execute a git submodule set-url some-path/ new-url (note the trailing slash /), then, instead of modifying existing submodule, the command will add another one.


Original answer (May 2009, ten years ago)

Actually, a patch has been submitted in April 2009 to clarify gitmodule role.

So now the gitmodule documentation does not yet include:

The .gitmodules file, located in the top-level directory of a git working tree, is a text file with a syntax matching the requirements -of linkgit:git-config3.
[NEW]:
As this file is managed by Git, it tracks the +records of a project's submodules.
Information stored in this file is used as a hint to prime the authoritative version of the record stored in the project configuration file.
User specific record changes (e.g. to account for differences in submodule URLs due to networking situations) should be made to the configuration file, while record changes to be propagated (e.g. +due to a relocation of the submodule source) should be made to this file.

That pretty much confirm Jim's answer.


If you follow this git submodule tutorial, you see you need a "git submodule init" to add the submodule repository URLs to .git/config.

"git submodule sync" has been added in August 2008 precisely to make that task easier when URL changes (especially if the number of submodules is important).
The associate script with that command is straightforward enough:

module_list "$@" |while read mode sha1 stage pathdo    name=$(module_name "$path")    url=$(git config -f .gitmodules --get submodule."$name".url)    if test -e "$path"/.git    then    (        unset GIT_DIR        cd "$path"        remote=$(get_default_remote)        say "Synchronizing submodule url for '$name'"        git config remote."$remote".url "$url"    )    fidone

The goal remains: git config remote."$remote".url "$url"