Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git - How to change url/path of a submodule

This is my .gitmodules:

[submodule "app/code/EthanYehuda/CronjobManager"]
        path = app/code/EthanYehuda/CronjobManager
        url = https://[email protected]/some_user/ethanyehuda_cronjobmanager.git

I need to change the url to https://github.com/Ethan3600/magento2-CronjobManager.git

So I just changed it:

[submodule "app/code/EthanYehuda/CronjobManager"]
        path = app/code/EthanYehuda/CronjobManager
        url = https://github.com/Ethan3600/magento2-CronjobManager.git

Then I added the file to the staging area and made a commit:

git add .gitmodules
git commit -m "change url of submodule xy"

Then I executed git submodule update --init. But if I go to app/code/EthanYehuda/CronjobManager and show the remote, then I still get https://[email protected]/some_user/ethanyehuda_cronjobmanager.git

like image 649
Black Avatar asked Jan 31 '20 12:01

Black


3 Answers

.gitmodules holds the suggested defaults, your changes will take effect when setting up subsequent clones.

Once somebody has done the clone though, the resulting repository's just a repository. You can go in to your existing submodule's directory and change its origin remote url the usual way, but the git submodule command has a handy shortcut,

git submodule sync

to fill in all the blanks for you.

sync [--recursive] [--] [<path>…​]

Synchronizes submodules' remote URL configuration setting to the value specified in .gitmodules. It will only affect those submodules which already have a URL entry in .git/config (that is the case when they are initialized or freshly added). This is useful when submodule URLs change upstream and you need to update your local repositories accordingly.

git submodule sync synchronizes all submodules while git submodule sync -- A synchronizes submodule "A" only.

If --recursive is specified, this command will recurse into the registered submodules, and sync any nested submodules within.

like image 95
jthill Avatar answered Oct 17 '22 21:10

jthill


Since git v2.25.0 (changelog), git submodule learn the new set-url command.

To use it simply do git submodule set-url -- <path> <url>

For you it is: git submodule set-url -- app/code/EthanYehuda/CronjobManager https://github.com/Ethan3600/magento2-CronjobManager.git

Note: wherever you are in your git the path should be relative to the top directory.

like image 32
alkino Avatar answered Oct 17 '22 23:10

alkino


See this answer for more information.

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
like image 6
abhiarora Avatar answered Oct 17 '22 23:10

abhiarora