Git submodule URL not including username?
Asked Answered
D

9

68

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)?

Digamy answered 10/10, 2011 at 14:33 Comment(3)
Do you own that server? Have you looked at gitolite? Can you just use public hosting?Nazarius
@Jefromi Bitbucket hosting currently, though gitolite looks like it would work for self-hosting.Digamy
See this: blog.tremily.us/posts/Relative_submodulesBurtburta
E
48

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.

Elizabethelizabethan answered 10/10, 2011 at 14:51 Comment(7)
That's good for system-wide configuration; in the edge case where you have a different username for git repo access than you do for other access, it's not idea. I found that removing the username from the repo URLs prompts for both username and password, so that works, if not ideal. If only git allowed something like a remote.origin.user config option...Digamy
Do you really use .netrc for other authentication?Elizabethelizabethan
I don't, but I don't know if others would; to me the ideal solution would be one completely within git and its configuration, without having to modify other parts of the OS, in case a user was using that file for another purpose that conflicted.Digamy
@MidnightLightning: I've added an alternative to my answer. Using SSH is generally more flexible than using HTTPS, in case that's an option.Elizabethelizabethan
@Digamy "I found that removing the username from the repo URLs prompts for both username and password, so that works, if not ideal." Can you provide an example of this, that sounds ideal to me. Or did you find another better solution? Thanks!Callas
Can someone explain this syntax: 'submodule.sub/foo.url'? In his example .gitmodules excerpt, 'foo' is a subsection of 'submodule'. Why doesn't the text I'm asking about read: 'submodule.foo.url'?Niemeyer
@Niemeyer - when you use git config to set some config, that's modifying .git/config. That's a separate config file with different sections and meaning from .gitmodules. In .git/config for this example there would be a section: [submodule "sub/foo"], containing url = https://myuser:[email protected]/git/foo.git. I hope that helps.Elizabethelizabethan
B
34

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.

Brewer answered 25/11, 2011 at 14:27 Comment(2)
It's not taking the same host it's taking the same URL e.g: ssh://[email protected]:29412/mainmodule/submoduleGoldagoldarina
If master-project url is [email protected]/team/project.git and submodule url is [email protected]/team/submodule.git, you can set .gitmodule submodule url to ../../team/submodule.git it worked for me. You could probably make the path shorter like this ../submodule.git. git submodule sync and git config -e might help you.Kibler
D
30

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
Der answered 15/3, 2016 at 12:55 Comment(3)
This is what fixed the issue for me. Thank you!Gerard
This also corrected the issue for me.Uncaredfor
I could not figure why my submodules was still asking for HTTP credentials after changing it to SSH, maybe something in the .git cache. Anyway, THIS worked !Patricepatrich
L
17

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.

Liberticide answered 5/1, 2016 at 18:39 Comment(1)
Works awesomely great! No hosts, ports or usernames anywhere except on the supermodule clone command line.Sices
A
6

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
Addict answered 29/10, 2012 at 15:44 Comment(2)
You're still specifying a user; now instead of assuming other maintainers have the password to the myuser account, you're assuming they have the password to the git user account, no?Digamy
Nope. There are Git servers that just look up your public key to match your user. Among those servers, there is Github, Atlassian Bitbucket Server (formerly Stash) and Atlassian Bitbucket (the SaaS version). This is neat because you don't have such issues with the username in the urlShoreward
B
6

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.

Bradford answered 18/3, 2019 at 17:48 Comment(2)
The great thing about this is you do it once and then everything just works, including "git clone --recurse-submodules". It does require the "YOUR_USERNAME" string to be consistent, though.Sices
If you do cloning on CI you can use env as well: GIT_CONFIG_PARAMETERS="'url.https://${SECRET_TOKEN}@github.com.insteadof=ssh://[email protected]'". Note additional quoting: '.Reinaldoreinaldos
N
2

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
North answered 1/7, 2014 at 11:55 Comment(3)
Is this suppose to work like that ? It works on windows but not on linuxMultitude
Actually I'm not sure if it is supposed to, but it works for me on Windows and Linux. It possibly depends on what the ssh stuff is doing in the background - as you don't give the user, it will have to use a default user.North
I found out more about that : Actually, your session username have to be the same as your ssh username for it to work. Or to get around this, you have to configure .ssh/config to use a specific username for each ssh serverMultitude
E
2

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
Eruct answered 29/5, 2019 at 16:35 Comment(0)
C
-2

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

enter image description here

Chandigarh answered 7/2, 2019 at 18:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.