James Cole front-end Developer

Setup a Private Git Server Roll your own on Digital Ocean

Of all version control systems, Git has proved most versatile and robust, at least for the needs of my employers and me. Currently, Github is by far the most popular hosted Git service, and is a wonderful way to make coding social or collaborate on someone else’s project.

However, you may find yourself working on project that you want to keep private for a while, and you don’t want to pay Github’s service fees for whatever reason. In this case, you want a private Git repo and server.

Though there are hosted alternatives to Github that allow this, like Bitbucket, this article outlines how to set one up on your private server provided by Digital Ocean.

+++

Assumptions

This article demonstrates how to setup a Git server on one of Digital Ocean’s Ubuntu droplets. However, the process should be similar for other Linux distros, excepting any distro specific commands.

It is also assumed your development machine uses Linux, OSX or some other UNIX variant. This article uses command line references, so hopefully you have some knowledge of that as well.

Development Machine Setup

On the development machine, you’ll want to create an SSH key from the command line.

1
2
ssh-keygen -t rsa
cat ~/.ssh/id_rsa.pub

The second line spits out your SSH key. Take note of it for now and we’ll add it to the server later.

Server Setup

If you have a bare server that you’ve never interacted with much, you’ll want to setup SSH access. It’s best to create a separate user to perform automated Git related actions, but I’ll leave that up to you and your discretion for now.

Configuring Access

With your SSH access, login to your remote server. Your credentials to achieve this are located in Digital Ocean’s admin panel.

After logging in, add your SSH key generated above to your list of authorized keys. You can do that by using your text editor of choice (vim, nano, etc) and pasting your key inside the file.

1
2
ssh server.com
vi ~/.ssh/authorized_keys

Since you’ve added your development environment’s SSH key here, you should be able to SSH in without entering your password. Try it out!

Next, pull in your updates then install Git. You may already have Git installed, and if so just skip this step. In case you’re not positive if Git is installed, just run which git at the command line to find out. If it’s installed, you’ll get the installation location returned. If not, time to install it!

1
2
sudo apt-get update
sudo apt-get install git-core

If you’re using this server by yourself, then you’re finished with the server setup. However, if you’ll be working on a team with other folks, you’ll want to do some additional configuration to allow that. This extra configuration is up to you, the reader, yet here are two resources to get started in that direction: Gitosis and Gitolite.

Configuring Repositories

Now that we’ve configured server, it’s time to create some repos! If you created a separate user for Git related activity, login as that user. Otherwise, you’re ready to begin.

Change directories to your user’s home. Then, create a repo using any name you want. Here, I just called it repo.git, but you can be as creative as you want!

Finally, change into this new Git directory and initialize a bare repository. That’s it!

1
2
3
4
cd ~
mkdir repo.git
cd !$
git --bare init

Codebase Setup

It’s now time to setup your codebase on your development machine. To start, exit from your server.

Change directories into your codebase. If you haven’t created a Git repository here, go ahead and do that now. Make sure to add in your files too.

If you’ve already created your Git repository, delete any origins in case they exist by running git remote rm origin. This is just a precaution to ensure everything is pushed to your personal server.

Next add your remote server and push in all your commit files. Note that the example uses root for the username, server.com for the server name or IP address (you can use either) and repo.git for the repository name. You’ll want to use your own values there.

1
2
3
4
git remote add origin root@server.com:repo.git
git push origin master
git config branch.master.remote origin
git config branch.master.merge refs/heads/master

The final two commands above simply set a default merge and remote.

tl;dr

Well, that wraps it up! I’ve demonstrated here a quick and easy way to setup your own Git server, so that you can maintain a private repository without paying any more than you already do for hosting.