How to Host Your Own Git Repository

There are several advantages to hosting your own git repository. Most obviously you can work around the additional cost associated with hosting private repos through github. Other benefits include all data residing on your personal server and not being controlled by someone else. The list goes on but if you are making a career out of writing code, you will eventually find some need to host your own personal version control repository. This article will walk you through setting up git on your hosted server, and getting your local computers pulling from and commiting to in under 15 minutes. While we are using git in this tutorial, the same general steps would be needed regardless of which Version Control System (VCS) you choose. Another great alternative to using git is Mercurial.

Without wasting any time, I'll go ahead and throw out there that the best transport protocol for accessing your private Git repo is ssh. This is because SSH is often already set up in your development environment, and if it isn’t, it’s easy to do. Most hosting companies offer ssh access as an add-on, a popular option is through PC-Gurus (Hostgator) for about $10/year. This method is secure and reliable so that's what we'll be using in this tutorial. If you intend to have your repository publicly available for the masses, ssh is not your best option since it requires authentication, even for read only purposes. If this is what you want then you should look into other protocol options such as the 'git' protocol. You can find information on this on The Protocols page at git-scm. See my other post on Setting up SSH keys if you need a reference. This article also assumes that you have ssh keys already configured between your local environment and your server. If you don't, no worries, the only difference is that you'll be prompted to enter you password after entering some of these commands.

Getting Started: Remote Setup

Open up a terminal window and enter the following:

#if using a standard port
ssh username@yourhost.com
#if using a non-standard port
ssh -p xxxx username@yourhost.com
mkdir -p ~/git-storage-dir/projectname.git
cd ~/git-storage-dir/projectname.git
git --bare init


Now, you have a self hosted repository! But it's emtpy, let's configure our local set up and add some files to our repository.

Local Setup

Pop open a terminal window and enter the following:

mkdir yourproject
cd yourproject
git init
#if using a standard port
git remote add origin ssh://username@yourhost.com/~/git-storage-dir/projectname.git
#if using a non-standard port
git remote add origin ssh://username@yourhost.com:xxxx/~/git-storage-dir/projectname.git
touch .gitignore
git add .
git commit -m "Initial Commit"
git push origin master


If you need to update the remote origin you can use

git remote set-url origin ssh://username@yourhost.com/~/git-storage-dir/projectname.git


Then run the following to verify you have the correct push/pull path.

git remote -v


That's it! You have your own git repo, feel free to push and pull to and from it as you like. In case you ran into troubles, here are a few links that may be of help.