Setup your own git hosting service

Last modification on 2018-03-25

This article assumes you use OpenBSD for the service files and OS-specific examples.

 

Why

A good reason to host your own git repositories is because of having control over your own computing. Assuming your hosting is secure you can be sure noone tampers with the data.

A particular disturbing example was the SourceForge ads/malware/hijack controversies.

The same thing can happen with Github, Bitbucket or other similar services. After all: they are just a company with commercial interests.

These online services also have different pricing plans and various restrictions, but when you host it yourself the restrictions are mostly the resource limits of the system and your connection, therefore it is a much more flexible solution.

 

Creating repositories

For the hosting it is recommended to use a so-called "bare" repository. A bare repository means no files are checked out in the folder itself. To create a bare repository use git init with the --bare argument:

$ git init --bare

I recommend to create a separate user and group for the source-code repositories. In the examples we will assume the user is called "src".

Login as the src user and create the files. To create a directory for the repos, in this example /home/src/src:

$ mkdir -p /home/src/src
$ cd /home/src/src
$ git init --bare someproject
$ $EDITOR someproject/description

Make sure the git-daemon process has access permissions to these repositories.

 

Install git-daemon (optional)

Using git-daemon you can clone the repositories publicly using the efficient git:// protocol. An alternative without having to use git-daemon is by using anonymous SSH, HTTPS or any public shared filesystem.

When you use a private-only repository I recommend to just use SSH without git-daemon.

Install the git package. The package should contain "git daemon":

# pkg_add git

Enable the daemon:

# rcctl enable gitdaemon

Set the gitdaemon service flags to use the src directory and use all the available repositories in this directory. The command-line flags "--export-all" exports all repositories in the base path. Alternatively you can use the "git-daemon-export-ok" file (see the git-daemon man page).

# rcctl set gitdaemon flags --export-all --base-path="/home/src/src"

Run directly the service as _gitdaemon:

# rcctl set gitdaemon user _gitdaemon

Instead of root and then changing uid as per default flag in /etc/rc.d/gitdaemon:

daemon_flags="--user=_gitdaemon"

Which will also avoid this warning while cloning:

"can't access /root/.git/config"

Now start the daemon:

# rcctl start gitdaemon

 

Cloning and fetching changes

To test and clone the repository do:

$ git clone git://yourdomain/someproject

if you skipped the optional git-daemon installation then just clone via SSH:

$ git clone ssh://youraccount@yourdomain:/home/src/src/someproject

When cloning via SSH make sure to setup private/public key authentication for security and convenience.

 

Pushing changes

Add the repository as a remote:

$ git remote add myremote ssh://youraccount@yourdomain:/home/src/src/someproject

Then push the changes:

$ git push myremote master:master

 

Git history web browsing (optional)

Sometimes it's nice to browse the git history log of the repository in a web browser or some other program without having to look at the local repository.

It's also possible with these tools to generate an Atom feed and then use a newsreader to track the git history:

My sfeed RSS/Atom parser and format programs.

 

Setting up git hooks (optional)

 

I hope this is useful. Have fun.