code and games
Built with Hugo and Hyde-Y.

Personal Hosted Git Server

· Read in about 2 min · (368 Words)

In an attempt to continue doing things right, I’ve put my blog in a git repository. I considered putting it on Github, whether public or private, but honestly I’ve been looking for an excuse to set up my own personal Git server. Normally I’d go with a self-hosted all-in-one system like Gitlab or gogs. We use Gitlab at work, and I love the minimal resource usage of gogs, but for now they’re just overkill for what I need.

I started out by following the directions outlined at this Digital Ocean article. My servers are hosted on Rackspace, but that doesn’t make any difference for the purposes of the steps outlined in the article. One difference for me, and is perhaps a missing step, is that I had to use makehomedir_helper git as root after I created the git user.

Once all of that was set up, I came up against the first pain point of self-hosted git: creating new projects remotely. In my ideal world, I’d be able to git remote add and git push and the remote server would sort itself out, but that’s not something git supports. One thing it does support, though, is custom git commands. If you have an executable in your PATH that starts with git- then it becomes a git command!

I whipped up a quick git-make-remote, which has a lot of room for improvement, but gets the job done:

#!/usr/bin/env bash

usage() {
cat <<HERE
usage: git make-remote <project-name>
   or: git make-remote <path/to/project-name>
HERE
}

GIT_SERVER="git.zikes.me"
GIT_USER="git"

case $# in
        1)
                ssh $GIT_USER@$GIT_SERVER "mkdir -p $1.git && cd $1.git && git init --bare"
                git remote add origin $GIT_USER@$GIT_SERVER:$1.git
                exit 0
                ;;
        *)
                usage && exit 1
                ;;
esac

In action:

$ git make-remote blog
Initialized empty Git repository in /home/git/blog.git/

$ git push origin master
Counting objects: 150, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (133/133), done.
Writing objects: 100% (150/150), 325.19 KiB | 0 bytes/s, done.
Total 150 (delta 5), reused 0 (delta 0)
To git@git.zikes.me:blog.git
 * [new branch]      master -> master

Future plans will be to modify the script to use custom git config variables for GIT_SERVER and GIT_USER, and/or get them as command arguments.