Host Your Own Private Git Repos

Mar 31, 2018

Hosting git repos on your own server is actually quite easy. Login to the server, create a new directory, and initialize a bare repo:

mkdir foo.git
cd foo.git
git init --bare

That's it! Now, from the client, clone this repo with:

git clone

Having a dedicated user for git repos on the server makes it easier share access to the repo. Create a new user git with a login shell restricted to git commands:

sudo adduser --shell $(which git-shell) git

Now create a repo in the home directory of the git user:

cd /home/git
sudo -u git mkdir bar.git
cd bar.git
sudo -u git git init --bare

As before, clone the new repo from the client using:

git clone

Backup the repos

This is my script to take daily backups of all the git repos on the server to Amazon S3.


set -e


renice -n 15 $$

trap "rm -f /tmp/gitbackup/*.git.tar.gz" EXIT

mkdir -p ${TMPDIR}
cd ${TMPDIR}

for proj in ${GITDIR}/*.git; do
    base=$(basename $proj)
    tar -C $GITDIR -zcf ${base}.tar.gz $base

export AWS_ACCESS_KEY_ID=xxxxx
export AWS_DEFAULT_REGION=us-west-2

aws s3 cp ${TMPDIR}/*.git.tar.gz s3://mygitbucket/

If the repos are large, it might be worthwhile checking whether the hash of the gzipped repo has changed before uploading. It's also good idea to use envdir to manage the access keys rather than putting them in the backup script.

Web front-end using cgit and nginx

Sometimes it's useful to view source code and commits on a web browser. cgit is an awesome light-weight webapp for this. Unlike heavy apps like GitLab, cgit needs no database, which reduces the administrative burden.

Install cgit, nginx, fcgiwrap, and apache-tools (to create a .htpasswd file).

sudo apt install cgit nginx fcgiwrap apache2-utils

Specify the location of the git repos and static assets in the cgit config at /etc/cgitrc.




To get syntax highlighting, install python-pygments and uncomment the source-filter option.

If you'd like to password protect access to, create a .htpasswd file:

sudo htpasswd /etc/nginx/.htpasswd <username>

This is my nginx conf file to serve cgit from

server {
    listen 80;
    listen [::]:80;


    location /.well-known/acme-challenge/ {
        root /var/www/;
    location / {
        return 301$request_uri;

server {
    listen 443 ssl;
    listen [::]:443 ssl;


    ssl_certificate /etc/letsencrypt/live/;
    ssl_certificate_key /etc/letsencrypt/live/;

    location /cgit-static/ {
        alias /usr/share/cgit/;

    location /cgit/ {
        auth_basic "Restricted";
        auth_basic_user_file /etc/nginx/.htpasswd;

        include fastcgi_params;
        fastcgi_split_path_info ^(/cgit)(.*)$;
        fastcgi_param   PATH_INFO        $fastcgi_path_info;
        fastcgi_param   SCRIPT_FILENAME  /usr/lib/cgit/cgit.cgi;
        fastcgi_param   QUERY_STRING     $args;
        fastcgi_param   HTTP_HOST        $server_name;
        fastcgi_pass    unix:/var/run/fcgiwrap.socket;

    location / {
        root /var/www/;

You might also want to restrict repo access to only whitelisted IPs.

Archive · RSS · Mailing list