Shared Git repository over ssh for multiple users

05-01-2015 | Remy van Elst


Table of Contents


This tutorial will show you how to set up a shared git repo with multiple accounts over ssh. Only ssh, not gitolite or any other software. This is usefull if you have a small team of people that don't often need access to the repo and don't want something like github or bitbucket. With this you can use existing infrastructure.

I have used this for a sysadmin team which had a few scripts scattered.

Folders and Permissions

First set up a new group for the users of the repository. You can use the groupadd command but I find it easier to edit /etc/group and add a line line this:

gitusers:x:2222:user1,user2,user3,user4

Do note that all the users you add above should have an account on the system. (adduser user/useradd user)

Now create a folder to house the repo's:

mkdir -p /var/git/repository1

Then change the group permissions on that folder:

chgrp gitusers /var/git/repository1
chmod g+ws /var/git/repository1

Repository

We can now create the repository. Navigate to the folder and initialize the repository:

cd /var/git/repository
git init --bare --shared=group

That's it. Now you can clone, pull, push and commit the repo like normal:

git clone git+ssh://user1@gitserver/var/git/repository1

Script

A simple script which makes the above easier. This assuming the group is gitusers and the root path is /var/git/.

#!/bin/bash
GITGROUP="gitusers"
REPOPATH="/var/git"
if [[ -z "$1" ]]; then
    echo "You must specify a repo name"
    echo "./$0 repo_name"
    exit 1
fi
mkdir "${PATH}/$1"
chgrp "${GITGROUP}" "${PATH}/$1"
chmod 2775 "${PATH}/$1"
pushd "${PATH}/$1"
git init --bare --shared=group
popd
echo "Created repo $1."
echo "Clone URL: git+ssh://$(hostname -f)${PATH}/$1"

If you need to trigger a Jenkins build after a commit you can add the following post-receive hook.


Tags: bash, development, git, repository, shared, ssh,