pg2455
pg2455

Reputation: 5168

Changing https-address of submodules in .git/config to ssh-address

I need to use ssh forwarding-agent to enable git cloning on remote servers. The repository has submodules which were originally added with their https addresses. In order for forwarding agent to work, I think I need to change those addresses to their corresponding ssh address.

Will changing these addresses in .git/config solve my problem?

Upvotes: 1

Views: 2604

Answers (3)

Adithya Bhat
Adithya Bhat

Reputation: 3752

Recently, github deprecated usage of git:// protocol and I wanted to update the submodules in my repository to use https:// instead of git://.

I did the following:

$ vim .gitmodules # Change all the git:// URLs into https:// URLs
$ git submodule init
$ git add .gitmodules
$ git commit -m "Update submodules"
$ git push origin main

You can do the same to convert URLs to ssh://.

Upvotes: -1

joanis
joanis

Reputation: 12262

The URL of the module is stored in .gitmodules at the root of your sandbox. This file is tracked as part of the Git repository. If you make the change here and commit it, it will be visible to other users of the Git repo.

When you call git submodule sync and then git submodule init, the URL is resolved and copied to .git/config. When you call git submodule update, the submodule is cloned and its URL is also found in .git/modules/<module-name>/config.

To permanently change the URL, edit .gitmodules and call git submodule sync and git submodule init again.

To temporarily change the URL, make these two changes instead:

Change the URL in .git/config for the submodule

Go inside the submodule and call:

git remote set-url origin <new-url-with-https>

The second command will update the URL in .git/modules/<module-name>/config which is the .git folder for the submodule.

EDIT: If the module and submodules all live on the same server, using relative URLs for your submodules will solve the problem once and for all: each sandbox will fetch the submodules using the same protocol the main repo was cloned with. See also: https://stackoverflow.com/a/68120599/3216427

Upvotes: 5

phd
phd

Reputation: 94676

Use git config --global url.<base>.insteadOf to substitute URLs on the fly. Something like

git config --global url.<new-url-with-https>.insteadOf git@<server>:<user>/<repo>.git

See more examples in https://stackoverflow.com/search?q=%5Bgit-submodules%5D+insteadof

Upvotes: 4

Related Questions