OpenBSD Git server setup

tags: bsd · tutorials

Git subdomain

It’s good to have your Git server under a subdomain, so that it looks like git.example.org. To do that, add a new CNAME record for the subdomain on your registrar. If you’re on Epik, don’t forget the trailing period after the domain name.

Host: git | Points to: example.org.

Setting up the Git server

Install git:

# pkg_add git

First create a git user and give it a password. If you don’t want the same hosts that exist in your current user’s authorized_keys to have access to the Git server, then add their keys manually in the git user’s .ssh/authorized_keys file. In my case, it’s just me who has access to the server, so I will not edit anything:

# adduser git
...
# cp -r $HOME/.ssh /home/git/
# chown -R git:git /home/git/.ssh

Try sshing to the server as git from your local computer just to test if it works:

$ ssh git@example.org 

If it worked, exit the session.

Set up a Git directory where all repositories will be stored at and cd into it:

# mkdir /var/www/htdocs/git.example.org
# chown -R git:git /var/www/htdocs/git.example.org
# cd /var/www/htdocs/git.example.org

Say I want to create a repository named repo. The structure your repositories should have is name.git, so in this case it’ll be repo.git. Create a bare repository inside it:

# mkdir repo.git
# git init --bare repo.git

Replace each text inside double quotes with what you want it to be:

# echo "John Doe" > repo.git/.git/owner
# echo "This is a repo" > repo.git/.git/description
# echo "git://git.example.org/repo.git" > repo.git/.git/url

To be able to interact with the Git server, the repository’s owner needs to be the git user you created earlier:

# chown -R git:git repo.git

Since sometimes the Git server by default might not be listening on port 9418 (the default Git port), which means that you’ll not be able to clone repositories from the Git server, you have to set up the daemon yourself using a massive git command. You can (in fact, you should) also use Git over HTTPS or SSH, but for now I’ll be using the default Git protocol for cloning and SSH for pushing.

# git daemon \
	--base-path=/var/www/htdocs/git.example.org/ \
	--export-all \
	--enable=receive-pack \
	--reuseaddr \
	--informative-errors \
	--verbose \
	--detach

Pushing changes to the server

On your local machine, make a repository for repo (the repository you created earlier) and add a few files so that you have something to commit and push to the server:

$ mkdir repo && cd repo
$ touch foo bar
$ git init
$ git add .
$ git commit -m "initial commit"

In order to be able to push you have to edit .git/config. If you’ve used GitHub before, you might have noticed that when you want to push, you push to origin; for this server I’ll use home, but you can use whatever else you want, it doesn’t really matter.

Append the following block to your repository’s .git/config and of course replace the domain and repository names with your own ones:

[remote "home"]
	url = git@git.example.org:/var/www/htdocs/git.example.org/repo.git
	fetch = +refs/heads/*:refs/remotes/home/*

Push to the Git server:

$ git push home master

Try also cloning the repository to make sure that everything works:

$ git clone git://git.example.org/repo.git

If that step was successful too, great, you now have a working Git server. :-)

Create a Web frontend with stagit(1).