Skip to main content

Raymii.org Raymii.org Logo

Quis custodiet ipsos custodes?
Home | About | All pages | Cluster Status | RSS Feed

Shared Git repository over ssh for multiple users

Published: 05-01-2015 | Author: Remy van Elst | Text only version of this article


❗ This post is over nine years old. It may no longer be up to date. Opinions may have changed.


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.

Recently I removed all Google Ads from this site due to their invasive tracking, as well as Google Analytics. Please, if you found this content useful, consider a small donation using any of the options below. It means the world to me if you show your appreciation and you'll help pay the server costs:

GitHub Sponsorship

PCBWay referral link (You get $5, I get $20 after you've placed an order)

Digital Ocea referral link ($200 credit for 60 days. Spend $25 after your credit expires and I'll get $25!)

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 , tutorials