Warm tip: This article is reproduced from stackoverflow.com, please click
git git-submodules

How to change url/path of a submodule

发布于 2020-03-31 23:00:09

This is my .gitmodules:

[submodule "app/code/EthanYehuda/CronjobManager"]
        path = app/code/EthanYehuda/CronjobManager
        url = https://company@bitbucket.org/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://company@bitbucket.org/some_user/ethanyehuda_cronjobmanager.git

Questioner
Black
Viewed
20
Black 2020-02-01 00:11

For me the solutions I found did not work, because the git history of my repository is completly different from the history of the new repository.

Let me explain. I received a project as a Zip file. I initialized a fresh repo and commited the files and pushed it to my bitbucket.

Then I found out, that it is a public github project. So I wanted to change the URL to the github repository. But they have completly different git histories (My repo just has an initial commit, while the github repo contains all commits.)

So it won't work by just changing the URL's.

So I had to delete the submodule and create it again.

Hint: <name_of_submodule> = app/code/EthanYehuda/CronjobManager (in my case)

Delete:

git submodule deinit <name_of_submodule>
git rm -f <name_of_submodule>
rm -rf .git/modules/<name_of_submodule>
git commit -m "Deleted submodule xy"

Re-Add:

git submodule add --force https://github.com/example/foo-bar.git <name_of_submodule>
git commit -m "Add submodul xy"

Fetching submodule app/code/EthanYehuda/CronjobManager error: The server refused requests to not specified object 9b677ef0e750acb9292030306bd97a3ee2734c61

↑ If an error like this shows up after pulling the project on a clone e.g. staging, then you have to sync and update --init after git pull:

git submodule sync
git submodule update --init