Only recently I’ve started to work with Git, don’t blame me I’m mainly a system administrator not a developer, and one of the things I’ve been asked to setup is a way to have a cloned Git project shared over SSH to a particular group of person that share the same linux group.
The issue is this setting are the permissions that must be properly set so you and the others don’t end up stomping on each other when pushing changes, so let’s see how to achieve this goal quickly.
Creating a new shared repository over ssh
If you just need to create a new git repository and share it via ssh with your co-workers you can issue the command:
git init --bare -shared myproject.git |
Explanation of the parameters:
–bare = When you create a git repository with the –bare option these 2 things happen:
- There is no working directory created.
- There is no .git directory is created. Instead, the files normally in the .git directory are placed in the top-level directory where the working directory would normally be, this is the suggested setup to setup a shared project
Check the following article to know why shared repositories should be bare repositories.
–shared = This option sets the permissions on everything in the repository to group-writable, possible values of this parameter are:
- umask (or false) – the default value. Git uses permissions reported by umask
- group (or true) – makes the repository group-writable
- all (or world or everybody) – same as group, but make the repository readable by all users
- 0xxx: 0xxx is an octal number and each file will have mode 0xxx. 0xxx will override users umask value. 0640 will create a repository which is group-readable but not writable. 0660 is equivalent to group.
You can verify the status of an existing Git repository looking in its config file, you should see a line containing:
[core] sharedRepository = true |
Please note that the value “true” is the same as “group”, and that this parameter can be set also with the command
git config core.sharedRepository true |
If you forgot to set it during the init phase.
Clone an existing Git project and share it over ssh
In this scenario you have first to clone an existing Git repository and than share it with your co-workers over ssh.
In this case it’s important that as first thing you configure the umask of the user that is going to do the clone operation, as this parameter will sets the initial permissions on the cloned git repository, to have a shared repository you have to set the files and directories to be group writable:
umask 002 git clone --bare remoterepository mydirectory.git cd mydirectory.git git config core.sharedRepository true |
Explanations of the commands and parameters:
umask 002 : This set the umask of the current user so all the new files created will have a 664 permission and directory 775.
git clone
–bare : This is equivalent of the –bare parameter in the init command, so
- There is no working directory created.
- There is no .git directory is created. Instead, the files normally in the .git directory are placed in the top-level directory where the working directory would normally be, this is the suggested setup to setup a shared project
remoterepository : this is the remote repository you want to clone such as: https://github.com/octocat/Spoon-Knife.git
mydirectory.git : The name of the directory where you want to clone the remote Git repository.
After the clone the next 2 commands just do a cd (change directory) in the cloned repository and make it a shared repository, so new files will have the correct permissions.
Conclusions
Hopefully this small article can help you in save some time in setting up a sharable Git repository, as you can see this it’s quite easy..once that you know the correct Git options and Linux commands 🙂
References:
Using Git with a central repository
Sharing a git repository over ssh
How do I share a Git repository with multiple users on a machine?
Popular Posts:
- None Found
One thing to mention if you decide later to share the repository. Just setting the core.sharedRepository to true is not enough. At least on Linux/UNIX (don’t know Windows), you must mark the directories as group writeable and setgid (to propogate the directory’s gid to new files), and set all files as group writable as well. What I do:
find . -type d -exec chmod g+ws {} \; #set directories as group writeable and setgid
find . -type f -exec chmod g+w {} \; #set files as group writeable
Good point John.
Thanks for sharing.