Git submodule URL not including username?

GitGit Submodules

Git Problem Overview


I have a git repository set up with several submodules, which creates a .gitmodules file that is a tracked file in the parent repository. However, there are other developers wanting to work on this repository, and check out the submodules. But currently the URLs for the remote submodule repositories contain my username; in the .gitmodules file it's something like:

[submodule foo]
  path = sub/foo
  url = https://[email protected]/git/foo.git

Obviously other developers can't fetch from example.com as myuser (they don't have my password); how can I have one main repository that multiple developers can pull/push to, and allow them to have individual access to the submodules (setting up a single username they all share on the submodule host server would work, but is not good user management)?

Git Solutions


Solution 1 - Git

If I understand correctly, you're using HTTP basic authentication over HTTPS to allow only particular developers to access the repository. In that case, you can commit a .gitmodules that looks like:

[submodule foo]
  path = sub/foo
  url = https://example.com/git/foo.git

... i.e. without a user name, and then tell each developer to put their username and password in their ~/.netrc file. (If you're using Windows, then there is some good advice on that here.) A simple .netrc file might look like:

machine example.com
  login myusername
  password areamandyingtotellsomeonehiscoolpassword

Update: An alternative, which doesn't involve using .netrc, would be the following:

Again, remove the user name from the URL in .gitmodules and commit and push that change. When someone clones the repository they would first run:

git submodule init

... which will set the config option submodule.sub/foo.url to the URL in .gitmodules. However, the init step won't clone the submodule into place until you do git submodule update, so you can do:

git config submodule.sub/foo.url https://myuser:[email protected]/git/foo.git

... and then:

git submodule update

To clone the submodules with the right user name. Note that then your username and password for HTTP authentication will be stored in your git config.

Solution 2 - Git

Actually you can specify a "relative" path to a submodule in .gitconfig:

[submodule foo]  
path = sub/foo  
url = ./git/foo.git

This url will reference same host (https://example.com) as the repository itself.

Solution 3 - Git

This threaded helped me, but I would add that after you modify the content of the file .gitmodules you need to execute the following command so git will pick it up:

git submodule sync

Solution 4 - Git

Antonk's answer won't work for teams because an individual's .gitconfig or .git/config is not under version control.

However it did lead me to experimenting with the url in .gitmodules.

[submodule foo]
path = sub/foo
url = ../foo.git

worked for me, assuming that the submodule repo is in the same url as the parent.

Solution 5 - Git

You can use ssh authentication.

just replace this

[submodule foo]
  path = sub/foo
  url = https://[email protected]/git/foo.git

with this

[submodule foo]
  path = sub/foo
  url = [email protected]:git/foo.git

Solution 6 - Git

You can try .insteadOf, e.g.

git config --global url."ssh://[email protected]:29418/".insteadOf "ssh://gerrit.foobar.com:29418/"

Each developer will have to do this once on each computer they work on.

Solution 7 - Git

I had this problem in combination with Gerrit (via ssh).
Removing the username worked,
so my .gitmodules looks like this now:

[submodule "sub/module1"]
	path = sub/module1
	url = ssh://servername:29418/module1

Solution 8 - Git

Modify your .gitmodules and remove the username from the url:

[submodule foo]
  path = sub/foo
  url = https://example.com/git/foo.git

And finally:

cd foo/
git submodule init
git submodule update

# Will be prompted to provide username and password

Solution 9 - Git

Alternatively you can use HTTPS in place of SSH which will prompt you for your username and password:

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMidnightLightningView Question on Stackoverflow
Solution 1 - GitMark LongairView Answer on Stackoverflow
Solution 2 - GitAntonKView Answer on Stackoverflow
Solution 3 - Gituser698116View Answer on Stackoverflow
Solution 4 - GitrhamiltonView Answer on Stackoverflow
Solution 5 - GitmkilicView Answer on Stackoverflow
Solution 6 - GitbviktorView Answer on Stackoverflow
Solution 7 - GitRomanView Answer on Stackoverflow
Solution 8 - GitGiorgos MyrianthousView Answer on Stackoverflow
Solution 9 - GitColinView Answer on Stackoverflow