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

3
  • Do you own that server? Have you looked at gitolite? Can you just use public hosting?
    – Cascabel
    Commented Oct 10, 2011 at 14:36
  • @Jefromi Bitbucket hosting currently, though gitolite looks like it would work for self-hosting. Commented Oct 10, 2011 at 15:45
  • See this: blog.tremily.us/posts/Relative_submodules
    – marknuzz
    Commented May 28, 2014 at 2:51

9 Answers 9

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.

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... Commented Oct 10, 2011 at 15:44
  • Do you really use .netrc for other authentication? Commented Oct 10, 2011 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. Commented Oct 10, 2011 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. Commented Oct 10, 2011 at 16:26
  • 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:[email protected]/git/foo.git. I hope that helps. Commented Jul 25, 2019 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.

2
31

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
3
  • 1
    This is what fixed the issue for me. Thank you!
    – Vanessa
    Commented Jul 3, 2023 at 20:21
  • 1
    This also corrected the issue for me.
    – Zack
    Commented Jul 17, 2023 at 14:22
  • 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 !
    – Lenormju
    Commented Nov 21, 2023 at 7:52
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.

1
  • 2
    Works awesomely great! No hosts, ports or usernames anywhere except on the supermodule clone command line. Commented Jun 3, 2020 at 11:43
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
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? Commented Oct 29, 2012 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
    Commented Nov 19, 2015 at 11:15
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.

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. Commented Jun 3, 2020 at 9:34
  • 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: '.
    – nouveu
    Commented Jul 7, 2023 at 8: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
3
  • Is this suppose to work like that ? It works on windows but not on linux
    – BlueMagma
    Commented Nov 10, 2015 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
    Commented Nov 11, 2015 at 9: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
    Commented Nov 11, 2015 at 9: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
-2

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

enter image description here

Not the answer you're looking for? Browse other questions tagged or ask your own question.