Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change git submodules url locally?

The original .gitmodules file uses the hard coded https urls but for some automated tests I clone from ssh and make the submodule urls relative as in ../ModuleName. I don't want to push these changes back into the repo either.

# change the https://github.com/ with [email protected]:
sed -i 's/https:\/\/github.com\//[email protected]:/g' ".git/config"
# delete the url lines from the submodule blocks of .git/config
sed -i '/submodule/ {$!N;d;}' ".git/config"
# change hardcoded https:// urls of submodules to relative ones
sed -i 's/https:\/\/github.com\/ProjName/../g' ".gitmodules"
# also make the same change in the .git/modules/*/config
sed -i 's/https:\/\/github.com\/ProjName/../g' .git/modules/*/config
# sync and update
git submodule sync
git submodule update --init --recursive --remote

With the snippet above, it does what I want. However, the annoying thing is, .git/modules/ folder doesn't seem to be under version control but if I just delete it, git submodule sync and most other Git operations just stop working.

Is there a way to get the .git/modules regenerated after modifying the .gitmodules and .git/config files?

like image 735
abdus_salam Avatar asked Feb 03 '17 16:02

abdus_salam


People also ask

Can I edit git submodule?

The submodule is just a separate repository. If you want to make changes to it, you should make the changes in its repository and push them like in a regular Git repository (just execute the git commands in the submodule's directory).

How do I manage git submodules?

Use the git submodule update command to set the submodules to the commit specified by the main repository. This means that if you pull in new changes into the submodules, you need to create a new commit in your main repository in order to track the updates of the nested submodules.

How do I rename a submodule?

Simply click on the submodule > Edit and you can rename the submodule in the new window that opens up.


1 Answers

If you want to modify the URL used for a submodule only for the local repo, then don't modify the .gitmodules file, that is only for changes that you want to push.

Instead, first initialize the local submodule config:

git submodule init

Then modify the .git/config file to change the submodule URL as usual. (Again, no need to modify .gitmodules here, and if this is a new clone you probably won't have .git/modules yet.)

As @jthill points out, an easier way to modify the submodule URLs is:

git config submodule.moduleName.url ssh://user@server/path

At this point you don't want to run git submodule sync, because that will overwrite the changes you just made with the values from the .gitmodules file. Instead, go straight to:

git submodule update --recursive --remote
like image 68
Scott Weldon Avatar answered Sep 28 '22 10:09

Scott Weldon