Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git submodules and ssh access

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?

like image 752
midtiby Avatar asked May 17 '11 13:05

midtiby


People also ask

What are git submodules for?

Git submodules allow you to keep a git repository as a subdirectory of another git repository. Git submodules are simply a reference to another repository at a particular snapshot in time. Git submodules enable a Git repository to incorporate and track version history of external code.

What does git pull -- recurse submodules do?

If you pass --recurse-submodules to the git clone command, it will automatically initialize and update each submodule in the repository, including nested submodules if any of the submodules in the repository have submodules themselves.

What is the advantage of using submodules?

The first important advantage to using submodules is that submodules allow you to keep the commit history of the component you're including in your project. Assuming the component you're adding is publicly available as a git repository, incorporating this component without the use of submodules presents a problem.


2 Answers

The other user has to alter the .git/config file to change the user name to his own username. That way, git uses the right user to connect to the server.

[submodule "path/to/module"]     url = ssh://otheruser@server/pathtorepos 
like image 23
Ikke Avatar answered Sep 28 '22 06:09

Ikke


If possible, it's best to make sure that the .gitmodules file contains a URL for the repository that can be cloned by anyone, typically either a git:// or http:// URL. Then users that have SSH access themselves can change into the submodule after cloning and change the URL in remote.origin.url to point to an SSH URL with their username, e.g.:

 cd my-submodule  git remote set-url origin otheruser@server:/pathtorepos 

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 .gitmodules if 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 .gitmodules are only used when initializing the submodule. Initializing the submodule sets the config value submodule.<SUBMODULE-NAME>.url in 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:

git config submodule.my-submodule.url otheruser@server:/pathtorepos 

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 origin within the submodule - at that point it's only useful to set the submodule.my-submodule.url config value in the main project if you're likely to be deleting and re-updating the submodule.

like image 116
Mark Longair Avatar answered Sep 28 '22 04:09

Mark Longair