65

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://myuser@example.com/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)?

MidnightLightning
  • 6,715
  • 5
  • 44
  • 68

9 Answers9

47

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:mypass@example.com/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.

Community
  • 1
  • 1
Mark Longair
  • 446,582
  • 72
  • 411
  • 327
  • 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... – MidnightLightning Oct 10 '11 at 15:44
  • Do you really use `.netrc` for other authentication? – Mark Longair Oct 10 '11 at 15:55
  • 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. – MidnightLightning Oct 10 '11 at 16:14
  • @MidnightLightning: I've added an alternative to my answer. Using SSH is generally more flexible than using HTTPS, in case that's an option. – Mark Longair Oct 10 '11 at 16:26
  • @MidnightLightning "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! – Polar Bear Aug 14 '17 at 23:57
  • 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'? – BobbyA Jul 24 '19 at 18:25
  • 1
    @BobbyA - 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:mypass@example.com/git/foo.git`. I hope that helps. – Mark Longair Jul 25 '19 at 19:16
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.

chenrui
  • 8,910
  • 3
  • 33
  • 43
AntonK
  • 1,210
  • 1
  • 16
  • 22
  • 2
    It's not taking the same host it's taking the same URL e.g: ssh://user@example.com:29412/mainmodule/submodule – Pratik Anand Sep 01 '16 at 16:38
  • If master-project url is https://user@host.org/team/project.git and submodule url is https://user@host.org/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. – Gregy8 Jun 08 '23 at 14:13
28

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
user698116
  • 392
  • 4
  • 4
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.

rhamilton
  • 473
  • 3
  • 13
6

You can use ssh authentication.

just replace this

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

with this

[submodule foo]
  path = sub/foo
  url = git@example.com:git/foo.git
mkilic
  • 117
  • 1
  • 2
  • 9
    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? – MidnightLightning Oct 29 '12 at 17:51
  • 2
    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 url – rcomblen Nov 19 '15 at 11:15
5

You can try .insteadOf, e.g.

git config --global url."ssh://YOUR_USERNAME@gerrit.foobar.com:29418/".insteadOf "ssh://gerrit.foobar.com:29418/"

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

bviktor
  • 1,356
  • 13
  • 14
  • 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. – StellarVortex Jun 03 '20 at 09:34
  • If you do cloning on CI you can use env as well: `GIT_CONFIG_PARAMETERS="'url.https://${SECRET_TOKEN}@github.com.insteadof=ssh://git@github.com'"`. Note additional quoting: `'`. – nouveu Jul 07 '23 at 08:44
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
Roman
  • 707
  • 8
  • 16
  • Is this suppose to work like that ? It works on windows but not on linux – BlueMagma Nov 10 '15 at 13:54
  • 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. – Roman Nov 11 '15 at 09:23
  • 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 server – BlueMagma Nov 11 '15 at 09:26
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
Giorgos Myrianthous
  • 36,235
  • 20
  • 134
  • 156
-2

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

enter image description here

Colin
  • 1,758
  • 1
  • 19
  • 24