1020

I've created a git repository with a submodule in it. I'm able to tell the submodule itself to change its remote repository path, but I'm not sure how to tell the parent repository how to change the remote repository path for the submodule.

I wouldn't be surprised if I'm somewhat out of luck and have to do things manually, as even deleting submodules isn't easy.

2
  • 42
    Note: Git 2.25 (Q1 2020) comes with a new command": git submodule set-url [--] <path> <newurl> Commented Dec 16, 2019 at 21:37
  • 4
    There's an answer for that below. Please upvote it! Commented Jan 20, 2021 at 16:44

8 Answers 8

1310

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 --recursive
cd <submodule_dir> 

git fetch
git checkout origin/master
git branch master -f
git checkout master
Sign up to request clarification or add additional context in comments.

15 Comments

This doesn't seem to update .git/config, at least in 1.7.1 or 1.7.3.
does this update the submodule url configuration for the previous commits also? ex if i checkout a older commit, will it point to new submodule urls?
Use git submodule foreach -q git config remote.origin.url to see "actual" submodule urls
It didn't update .git/config for me using git 2.1.0. I had to update both .gitmodules and .git/config manually before running a git submodule sync --recursive to have my submodule remote be updated.
This appears to be missing the key step of git submodule update --init --recursive --remote which actually changes the repository to the new remote
|
346

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>

(On the -- separator, see "double hyphen as a signal to stop option interpretation and treat all following arguments literally")

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:

# Usually causes unexpected behavior
git submodule set-url some-path/ new-url

(note the trailing slash / in some-path/), then, instead of modifying existing submodule, the command will add another one. Instead run:

# Good
git submodule set-url some-path new-url

That replaces the pre-Git 2.25 commands, which were:

git config -f .gitmodules submodule.<submodule_path>.url <new_url>
git config -f .git/config submodule.<submodule_path>.url <new_url>
git submodule sync
git submodule update --init --remote

Original answer (May 2009, fourteen years ago)

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

So now the .gitmodules 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-config4.
[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 path
do
    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"
    )
    fi
done

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


Note:

Git 2.40 (Q1 2023) clarifies git config remote.<remote>.url:

See commit d390e08 (07 Feb 2023) by Calvin Wan (CalvinWan0101).
(Merged by Junio C Hamano -- gitster -- in commit 59397e9, 15 Feb 2023)

Documentation: clarify multiple pushurls vs urls

Signed-off-by: Calvin Wan

In a remote with multiple configured URLs, git remote -v(man) shows the correct url that fetch uses.

However, git config remote.<remote>.url(man) returns the last defined url instead.

This discrepancy can cause confusion for users with a remote defined as such, since any url defined after the first essentially acts as a pushurl.

Add documentation to clarify how fetch interacts with multiple urls and how push interacts with multiple pushurls and urls.

urls-remotes now includes in its man page:

The <pushurl> is used for pushes only.

It is optional and defaults to <URL>.

Pushing to a remote affects all defined pushurls or to all defined urls if no pushurls are defined.
Fetch, however, will only fetch from the first defined url if muliple urls are defined.


Git 2.44 (Q1 2024), batch 10, tightens URL checks fsck makes in a URL recorded for submodules.

So when you change the URL, the URL validity check is now more robust.

See commit 8430b43, commit 7e2fc39, commit 6af2c4a, commit 13320ff (18 Jan 2024) by Victoria Dye (vdye).
(Merged by Junio C Hamano -- gitster -- in commit 76bd129, 26 Jan 2024)

submodule-config.c: strengthen URL fsck check

Signed-off-by: Victoria Dye

Update the validation of "curl URL" submodule URLs (i.e.
those that specify an "http[s]" or "ftp[s]" protocol) in 'check_submodule_url()' to catch more invalid URLs.
The existing validation using 'credential_from_url_gently()' parses certain URLs incorrectly, leading to invalid submodule URLs passing 'git fsck'(man) checks.
Conversely, 'url_normalize()' - used to validate remote URLs in 'remote_get()' - correctly identifies the invalid URLs missed by 'credential_from_url_gently()'.

To catch more invalid cases, replace 'credential_from_url_gently()' with 'url_normalize()' followed by a 'url_decode()' and a check for newlines (mirroring 'check_url_component()' in the 'credential_from_url_gently()' validation).

20 Comments

What does the optional double dashes in git submodule set-url [--] <path> <newurl> do?
@jeverling They help separate options from parameters: see stackoverflow.com/a/1192194/6309
Note that for Ubuntu users with an older version git you can use this PPA to update: launchpad.net/~git-core/+archive/ubuntu/ppa
This should be the preferred answer now. One simple command for everything.
What does [--] mean? Can you give an example?
|
205

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.git
git config --file=.gitmodules submodule.Submod.branch Development
git submodule sync
git submodule update --init --recursive --remote

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

6 Comments

This worked but I had to remember to push the changes to the remote. git add .gitmodules git commit -m "modified submodule URL" git push origin master
Well, that created a terrible mess for me. Commands went down quietly, but the actual submodule repository still thinks its remote is the old one (the old URL). Maybe these commands should be accompanied with other commands within the submodule's repository?
The last command is a bit extreme... If you have submodules with submodules inside, this will remotely update the sub-submodules as well, which is unlikely what you need.
Note that you need replace Submod with name of your submodule!
This is less risky than going into your .gitmodules file manually. thx 👍
|
168

In simple terms, you just need to edit the .gitmodules file, then resync and update:

Edit the file, either via a git command or directly:

git config --file=.gitmodules -e

or just:

vim .gitmodules

then resync and update:

git submodule sync
git submodule update --init --recursive --remote

1 Comment

git submodule update --init worked for me, --remote seems to tie it to the HEAD of the remote repo.
89

What worked for me (on Windows, using git version 1.8.3.msysgit.0):

  • Update .gitmodules with the URL to the new repository
  • Remove the corresponding line from the ".git/config" file
  • Delete the corresponding directory in the ".git/modules/external" directory (".git/modules" for recent git versions)
  • Delete the checked out submodule directory itself (unsure if this is necessary)
  • Run git submodule init and git submodule update
  • Make sure the checked out submodule is at the correct commit, and commit that, since it's likely that the hash will be different

After doing all that, everything is in the state I would expect. I imagine other users of the repository will have similar pain when they come to update though - it would be wise to explain these steps in your commit message!

5 Comments

Thank you so much for this. This is the only one that worked for me after I had already run a git submodule update. Following the other answers wouldn't change what was in the ./git/modules/external dir so attempting to update would result in it still pulling the incorrect url.
this seems to be a bit dangerous, and I'm not sure it preserves the history of the previous submodule. If, for instance, you want to check out an old commit or branch of your main-repository (the one containing the submodule) I'm not sure it will know to pull the OLD submodule attached and related to that old commit of the main.
No, it almost certainly won't know - you'll have to do all the steps after the first one again. This is just what I found worked for nuking the current state of the submodule. I don't know if the state of things has changed since I wrote this, mind :)
@MottiShneor that seems dangerous indeed if you need to keep the previous submodule history, though I'm not sure about it. In my case it's the only solution that worked, what I wanted was basically replace the original submodule with my own fork
Followed these steps and found that the "Delete the checked out submodule directory itself (unsure if this is necessary)" is necessary otherwise you will encounter "fatal: Not a git repository: ..." when running git submodule update
15

Just edit your .git/config file. For example; if you have a "common" submodule you can do this in the super-module:

git config submodule.common.url /data/my_local_common

1 Comment

This is only the best way if you are trying to change the URL for a one time use, not permanently in the super project. E.g. you want to clone submodules from local copies on disk.
3

git config --file=.gitmodules -e opens the default editor in which you can update the path

Comments

-3

A brute force approach:

  • update the .gitmodules file in the supermodule to point to the new submodule url,
  • add and commit the changes to supermodule/.gitmodules,
  • make a new clone of the supermodule somewhere else on your computer (making sure that the latest changes to the .gitmodules file are reflected in the clone),
  • change your working directory to the new clone of the supermodule,
  • run git submodule update --init --remote path-to-submodule on the submodule,

et voilà! The submodule in the new clone of the supermodule is properly configured!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.