Get your own Git Server using OpenBSD

       525 words, 3 minutes

When you want to host your code using Git but want to keep it secret, you can setup a Git service on your OpenBSD server. You’re then able to access it through the Wild Wild Web using SSH and don’t have to pay GitHub to expect privacy.

Configure a Git Server

I thought it would be complicated, but it’s not… A Git Server is basically a file tree that’s being managed and accessed by a git command. There are “hidden” files and special filenames that trace the sources structure and life. But it ’s basically a folder and it’s content.

On the OpenBSD server, start by installing Git:

# pkg_add git
quirks-2.304 signed on 2017-04-02T15:01:33Z
git-2.12.2:cvsps-2.1p0: ok
git-2.12.2: ok
The following new rcscripts were installed: /etc/rc.d/gitdaemon
See rcctl(8) for details.
Look in /usr/local/share/doc/pkg-readmes for extra documentation.

I don’t want to run Git as a daemon. I’ll access it via SSH. So that’s all for me!

Create a projet repository

On the server side, a projet is a sub-directory containing a Git structure:

# mkdir -p /home/git/project1
# /home/git/project1
# git init --bare
Initialized empty Git repository in /home/git/project1/

Another step done. That’s what the folder looks like:

# find ./
./
./hooks
./hooks/pre-push.sample
./hooks/multimail
./hooks/multimail/post-receive.example
./hooks/multimail/git_multimail.py
./hooks/multimail/migrate-mailhook-config
./hooks/pre-rebase.sample
./hooks/prepare-commit-msg.sample
./hooks/update.sample
./hooks/pre-receive.sample
./hooks/pre-applypatch.sample
./hooks/setgitperms.perl
./hooks/applypatch-msg.sample
./hooks/commit-msg.sample
./hooks/post-receive-email.sample
./hooks/post-update.sample
./hooks/pre-commit.sample
./info
./info/exclude
./branches
./description
./refs
./refs/heads
./refs/tags
./objects
./objects/pack
./objects/info
./HEAD
./config

Create a local repository

We can set up a local repository where the code will be modified and tested before being submitted:

# mkdir $HOME/git-copy/project1
# cd $HOME/git-copy/project1
# git init
# git add .
# git config user.name "Joel Carnat"
# git config user.email joel [at] carnat [dot] net
# git remote add origin /home/git/project1

The file can now be pushed and pulled to the server side:

# cd $HOME/git-copy/project1
# touch index.html
# git add index.html
# git commit -m "Initial: index.html" -a
[master (root-commit) dcb9525] Initial: index.html
 1 file changed, 1 insertion(+)
 create mode 100644 index.html
# git push origin master
Counting objects: 3, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 265 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To /home/git/project1
 * [new branch]      master -> master

Whenever I want to look at the server side, I can go to the repository directory and issue:

# git ls-tree --full-tree -r master
100644 blob b89bead3bf3c6cedfea645e742276daef2cbc23b    index.html

I can now double-check that my file was pushed successfully.

Create a remote repository

Time to set up remote access to the project repository. I use SSH to connect to the OpenBSD server. When logged in, I can access the Git repository filesystem:

# mkdir ~/drive_d/GITthings/project1
# git init
# git add .
# git config user.name "Joel Carnat"
# git config user.email joel [at] carnat [dot] net
# git remote add origin ssh://me@git-server.tld/home/git/project1
# git pull origin master
Enter passphrase for key '~/.ssh/id_rsa':
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
Depuis ssh://git-server.tld/home/git/project1
 * branch            master     -> FETCH_HEAD
 * [nouvelle branche] master     -> origin/master

And that’s all. Just as simple.