Git - How to change url/path of a submodule
Asked Answered
C

5

26

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

Coit answered 31/1, 2020 at 12:0 Comment(1)
Does this answer your question? How to change the remote repository for a git submodule?Fanlight
C
5

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
Coit answered 31/1, 2020 at 13:46 Comment(0)
A
35

.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.

Annexation answered 31/1, 2020 at 14:10 Comment(9)
Does not work. I changed the URL of another repository (app\code\Yireo\Webp2) with git remote set-url origin https://github.com/yireo/Yireo_Webp2.git. Then I executed git submodule sync. But the file .gitmodules was not updated with the new URL.Coit
No, you have to have already updated .gitmodules, then submodule sync. As the quoted docs say, sync updates the remote settings from what's in .gitmodules.Annexation
I guess this solution will work, if the git history is not entirely different.Coit
Huh? How would that matter? Git looks for the commit recorded for a path in the history available there. Switch urls to a new repo, fetch the history there, done. Now your submodule repo has the histories from the old upstream and the new one, there's nothing even a little bit wrong with that.Annexation
Because If I have two entirely different git repos as in my case and I do it like you describe, and then execute git log in the submodule, then I still see the commits of the old repo, even though the remote url changed. I solved it here https://mcmap.net/q/513611/-git-how-to-change-url-path-of-a-submoduleCoit
git log works from refs unless you tell it something different. Submodule commits are checked out directly. You're bringing a lot of preconceived notions to the table, and they're confusing you. If you have two entirely different git repos and do like I describe, git submodule update will work just fine, because it fetches the new history then checks it out.Annexation
I even deleted the .git folder inside of the submodule, then I executed git submodule sync followd by git submodule update but git log still shows the "initial commit" of the old repo and nothing else.Coit
Case in point: that wasn't a directory, you just deleted a pointer to the untouched submodule repo inside your main project's .git, and git submodule update restored the pointer.Annexation
So how can I solve this correctly? So far only my method works.Coit
T
26

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.

Tetralogy answered 26/10, 2021 at 14:17 Comment(0)
I
9

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
Imperceptible answered 31/1, 2020 at 12:9 Comment(2)
This looks way more complicated than it should be. Is this the official way?Coit
I had to change an existing URL in a submodule (adding an access token to it), so i not only had to set the value, but also read it. That is only possible using git config --file=.gitmodules -get [...], as the submodule command has no get-url option. Then set the modified value and sync, like shown here.Gignac
C
5

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
Coit answered 31/1, 2020 at 13:46 Comment(0)
I
1

You need delete and resync your submodule after updating the url.

Please refer to this answer. https://mcmap.net/q/13078/-how-do-i-replace-a-git-submodule-with-another-repo

Immunotherapy answered 31/1, 2020 at 12:6 Comment(4)
Does not work. Tried it step by step... I get error: The server rejects a request for an object not offered 86bbb310a268106bae244bc50bb6232fa8f0727f.Coit
"fetch" executed in submodule path 'app / code / Fooman / GoogleAnalyticsPlus', but 86bbb310a268106bae244bc50bb6232fa8f0727f not contain. Requesting this commit directly failed.Coit
I think the error shows, because my first repository is my own which only has a initial commit, and the new one is with a whole new git history from githubCoit
If you are not changing to a repository with a completly other git history, then I guess your method works.Coit

© 2022 - 2024 — McMap. All rights reserved.