Git submodules using relative urls

2020-07-08 00:44发布

问题:

I use git (TortoiseGit 1.7.5.0) and have a repo with submodules, which are referenced via absolute paths in a manner:

[submodule "common/sub"]
path = common/sub
url = ssh://localhost/lib/common/sub

While the super-repo is cloned as ssh://user@localhost/super, the username shouldn't go into each submodule url, because different users should work with the code using their own credentials.
When pulling/pushing submodules I had to enter my login (the password is not needed because I'm running Pageant): the username isn't passed to TortoisePlink via command line.

Surfing the Web, I found several posts mentioning relative urls for submodules, but apparently such approach doesn't work well with TortoiseGit: I tried several formats - the program often crashes. Anyway I succeeded with the format like:

[submodule "common/sub"]
path = common/sub
url = ../common/sub

The file .git/config is populated with the proper absolute urls, but after that TortoiseGit just crashes...

I've reviewed other options including:

  1. use gitolite (as proposed here) or sdorra (as proposed here), but this looks like an overcomplicated solution.

  2. follow subtree merge strategy (as proposed here). It looks like a nice approach.

One of the working workarounds is to initialize the submodule with a "non-standard" URL as explained at the end of git Submodules Explained (the link was found here).

I suppose some simpler workaround may help in my case (like providing default login in some local config file or via command line), but I haven't found any workable hints for that.

And the question: what is the easiest way of using submodules without hard-coded user names in absolute urls?

回答1:

Actually relative urls works fine with msysgit already. I've upgraded it from 1.7.7.1 to 1.7.8, and crashes are gone. Great! :)



回答2:

Instead of git submodules or the subtree merge strategy (which has a bit of a cumbersome workflow to master), there's actually a contrib to git called, "git subtree" which simplifies the whole process.

This would allow your super-project to have a clone of the sub-projects internally which can be split back out if you wish to contribute. This simplifies the cloning entirely for anyone else getting your project as they won't need to deal with extra fetches from various repositories with different credentials.

You can read about the contrib "git subtree" here: https://github.com/git/git/tree/master/contrib/subtree (be sure to check git-subtree.txt)

Also, since you tagged tortoisegit, you might be interested to know I'm working on contributing support for subtrees right now in a tortoisegit fork https://github.com/johnb003/TortoiseGit/tree/subtree-add



回答3:

Since you're using SSH as a transport you can use ~/.ssh/config to set a default username for the specific host:

Host git.example.org
    User my-git-account