Changing remote repository for a git submodule
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.
7 Solutions collect form web for “Changing 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 to reflect that change to the superproject and your working copy.
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
Actually, a patch has been submitted in April 2009 to clarify
So now the gitmodule documentation does not yet include:
.gitmodulesfile, located in the top-level directory of a git working tree, is a text file with a syntax matching the requirements -of linkgit:git-config1.
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"
What worked for me (on Windows, using git version 1.8.3.msysgit.0):
- Update .gitmodules with the path to the new repository
- Remove the corresponding line from the “.git/config” file
- Delete the corresponding directory in the “.git/modules/external” directory
- Delete the checked out submodule directory itself (unsure if this is necessary)
git submodule initand
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!
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
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
then resync and update:
git submodule sync git submodule update --init --recursive --remote
git config --file=.gitmodules -e opens the default editor in which you can update the path