I have some trouble with a git repository that contains several submodules.
The super git repository was constructed with the commands
mkdir projectname
cd projectname
git init
git submodule add ssh://myusername@server/pathtorepos
When a different user (“otheruser”) then clones the super repository everything seems to work out. But when it is time to get access to the submodule
git submodule init
git submodule update
git tries to clone the submodule using “myusername” instead of “otheruser”.
How to solve this problem?
If possible, it’s best to make sure that the
.gitmodulesfile contains a URL for the repository that can be cloned by anyone, typically either agit://orhttp://URL. Then users that have SSH access themselves can change into the submodule after cloning and change the URL inremote.origin.urlto point to an SSH URL with their username, e.g.:The other user should be able to do that even in the current situation. Update: Chris Johnsen points out below that it’s also reasonable to use an SSH URL in
.gitmodulesif you omit the username and all the users of the repository will have SSH access – they’ll need to add their username similarly to the above if it differs locally and remotely.Note that the URLs in
.gitmodulesare only used when initializing the submodule. Initializing the submodule sets the config valuesubmodule.<SUBMODULE-NAME>.urlin the main project to whatever’s committed in.gitmodules– this is the value that will be used on the first submodule update. Between initializing and updating the submodule, you can also change this URL that will be used for that first update with a command like:Indeed, you may need to do this if the first update fails. Once the submodule has been updated for the first time, the URL you need to change is that defined for
originwithin the submodule – at that point it’s only useful to set thesubmodule.my-submodule.urlconfig value in the main project if you’re likely to be deleting and re-updating the submodule.