To use Git alone, without other collaborators, the Git binary is sufficient. If we have multiple projects and each has different collaborators or different permissions, using a management system is helpful, even a minimal one. Gitolite is such a management system for Git projects. This article covers setting it up both directly on a machine and inside a Docker container for a portable, reproducible git hosting setup.

Installing Gitolite

On the machine that will host gitolite, create a user called git. In that user's home directory clone the gitolite project from GitHub, run the install procedure, and declare your public key as the administrator key.

sudo adduser git
su - git
mkdir -p ~/bin

git clone git://github.com/sitaramc/gitolite
gitolite/install -ln /home/git/bin
gitolite setup -pk id_rsa.pub

Usage

After installation, gitolite makes available for the administrator's key a project called "gitolite-admin". This project contains a "keydir" directory where SSH keys for access will be added, and a file "conf/gitolite.conf" which contains the actual permissions for each project.

To administer gitolite, clone the gitolite-admin project, add keys for collaborators, edit the config, then push. Gitolite will create new projects on the server and update access permissions automatically.

git clone git@git.rohost.com:gitolite-admin

# add keys to keydir/, edit conf/gitolite.conf

git add .
git commit -a -m "informative message"
git push

To clone all projects you have access to:

ssh git@git.rohost.com | grep "^ R" |\
awk '{print $NF}' | while read i; do\
mkdir -p $i && cd $i &&\
git clone git@git.rohost.com:$i .\
&& cd -; done

Wild Repos

Wild repos are projects that can be created by each user individually, without administrator intervention. To modify permissions:

ssh git perms -h

ssh git@host perms -l <repo>
ssh git@host perms <repo> - <rolename> <username>
ssh git@host perms <repo> + <rolename> <username>
# common <rolename> values: READERS, WRITERS

Deleting Projects

Only wild repos can be deleted. Gitolite allows both permanent deletion and moving to a Trash sector. To permanently delete a project it must first be unlocked:

ssh git@host D unlock <repo>
ssh git@host D rm <repo>

To send to trash instead (no unlock needed):

ssh git@host D trash <repo>

Docker Setup

Packaging Gitolite in Docker gives you a portable, reproducible git server that can be rebuilt from scratch on any machine. This setup assumes you have a base Docker image with SSH access (see Docker 101). The SSH key used for the base image becomes the gitolite administration key automatically.

In a new directory called gitrepo, create the Dockerfile:

FROM base
MAINTAINER Cosmin L. Neagu <cosmin@rohost.com>

ENV DEBIAN_FRONTEND noninteractive
RUN apt-get install -y git && \
    adduser --system --group --shell /bin/bash git && \
    cp /root/.ssh/authorized_keys /home/git/admin.pub && \
    cd /home/git; su git -c "mkdir -p bin" && \
    cd /home/git; su git -c "git clone git://github.com/sitaramc/gitolite" && \
    cd /home/git; su git -c "gitolite/install -ln" && \
    cd /home/git; su git -c "bin/gitolite setup -pk admin.pub"
docker build -t="gitrepo" .
docker run -d -p 127.0.0.1:222:22 gitrepo

Using the Git Server

Add a Host entry to ~/.ssh/config so you can use ssh git instead of typing the full address every time. If the IP, port, or user changes in the future, only this file needs updating.

Host git
    User git
    HostName 127.0.0.1
    Port 222
    RequestTTY no
# list projects we have access to
ssh git

# clone the administration project
git clone git:gitolite-admin

Creating a New Repo

Add the repo to conf/gitolite.conf inside the gitolite-admin clone, push, then clone the new repo.

repo TEST1
    RW+     =   admin
git commit -a -m "New repo: TEST1"
git push

ssh git              # confirm TEST1 appears
git clone git:TEST1

Importing from GitHub

Create the target repo on the server first, then mirror it over. The imported project will be accessible under its new name.

git clone https://github.com/fabric/fabric.git
cd fabric
git remote add gitolite git:TEST1
git push gitolite master
git push gitolite --all
git push gitolite --tags

Backup

It is absolutely necessary to have a backup strategy for the data in gitolite. The gitolite repositories live under the git user's home directory and can be backed up with any standard rsync or snapshot approach.