How to Setup Your Own Private Git Server

If you are a developer at any level, you likely use git and GitHub to help manage your code revisions.  GitHub is an excellent tool, and a great way for all of us to share our code.  However, unless you pay for one of GitHub’s private plans, all of your code is public.  If you’re like me, you might have some projects that you don’t feel comfortable sharing publicly on GitHub; or perhaps you’d simply like certain code to be under your own control and not stored in a cloud service.  Regardless of the reason, this article will show you how to setup your own git server either on your local network or on a hosted VPS type system.  You’ll be able to use it just like you’d use GitHub…either with the git command line tools or with any of the various git GUI clients.  You can even use GitHub’s GUI client with it.


Note: This particular configuration is suitable for a an individual developer or small team of developers who all share access to the same projects.  If you need to control access on an individual user or project basis, this solution is not for you.  While it can be done using this method, it requires setting up separate users, groups, and getting creative with some permissions.   If you truly need a flexible on-site Git server with full user management and control, I highly suggest GitLab.  


In this example, I assume you have the git command line tool installed on your developer workstation and it is available in your path.  If you need to install git, you can get it here.


Let’s get started…



Install git on the server

The first thing we need to do is actually install git on the server.  I’m going to assume an Ubuntu/Debian distribution, but if you’re using some other flavor, substitute your distro’s specific package management commands.


Configure the git user as well as the repos and .ssh directories

Next, we need to create a user that this git process will run as.  I recommend you create a user called “git”.  Create the user and then either login as this new user or do as I’ve done here and simply su as git.

I like to create a directory specifically for the repos, otherwise they’ll go into the root of git’s home directory which can be messy.  Call it whatever you like, but make sure there are no spaces or special characters. This directory will be part of the git URL you use to push/pull so I recommend you keep it short and simple.

Next we need to create the “.ssh” directory inside the git user’s home directory which will hold the SSH public keys of the various developers who will connect to the git service. We’ll also set some permissions.

Next, we’re going to create an empty “authorized_keys” file which will hold the public keys of the developers who are allowed to log into the git service.  We’ll also secure the access to this file

Next, you need to paste the PUBLIC keys of the developers who are allowed to connect to this git service.  If you’re on Windows and already using git, your public key is likely installed in C:\Users\<UserID>\.ssh as

2016-09-24 13_25_30-C__Users_Dave_.ssh


Edit this file with a text editor such as notepad or notepad++, highlight the text as shown here, copy. then paste into your authorized_keys file.

2016-09-24 - Notepad++


Note: If you don’t have an SSH key pair, I recommend using the PuTTYgen utility which comes with PuTTY to generate SSH key pairs.  You can also generate your key pairs on the server, but I prefer to generate them on my local machine so that my private key never touches the cloud.



Creating a project

When you’re ready to finally create a project, it’s a 2-step process which I’ll detail below.  Basically you create a directory on the server in the repos directory, and initialize it.  Then you create the same folder on your workstation, initialize it, commit it, and push it to the server to populate the bare repo. You will need to do this every time you want to create a project.  I do have a way to automate this (which I’ll outline in another post) but first let’s understand the manual process.



Creating a repository on the server

As before ensure you’re logged in as or su’d as the git user. Create a directory and run “git init –bare”


Creating the local repository

Next you create a local folder on your workstation, initialize it, fill it with some files. (I create a simple readme file here, but if you have files already feel free to add them at this point).  You then make a commit, add the remote and push.

Now you can use git as you normally would.  If another team member needs to clone the project, they simply need their public key added to the authorized_keys file and they run a git clone command like this.


At this point, everything should work as usual.  Any git-aware client or code editor should recognize the directory as a git repository and the various git functions should work as expected.


That covers the basic setup of the private git server.  Stay tuned for a follow up post on extending this configuration with some automation and other functionality.