Hosting a Git repo on a VPS

June 1, 2017

Hosting a Git repo on your own VPS server is a simple. Though paying for GitHub to host a private repo does not cost very much you may have reasons to keep your source repositories on your own server. You might enjoying do things yourself, have reasons to keep your source on your own server or would like to simply have a place to manage your personal projects. The following will walk you through the process of creating and hosting a git repository on your own server.

Server

On your server create a directory where you will store your repository. I'd image you'll create more than one over time so name the directory something appropriate such as repos. If you are just starting off and it is only you I suggest for simplicity you create a directory called repos under your home directory.

For our example here I'm going to create a repo called project on a machine with the domain name of example-server.com. My username for this example will be user. Before continuing make sure you can ssh easily into your account on your server. In our example you would need to be able to execute the following command.

$ ssh user@example-server.com

For this you should configure your ssh so you can do so without having to enter your password.d

Creating the repository

On the server the command to create the initial empty repository is git init –bar REPONAME.git. For example:

$ cd $HOME
$ mkdir repos
$ cd repos
$ git init --bare project.git

Local

With your server repository created it is now time to either clone your new repo or to point an existing local repo to it. Let's start with cloning.

When you clone the repo you'll be starting with a local repo and the upstream branch will be setup. In this scenario you haven't started working locally and want a blank slate.

$ git clone user@example-server.com:repos/project.git

If you've already started a project locally and want to start pushing your changes to your server you'll want to setup a remote to your server. Assuming you just have a directory with your project files will your require to do a few things starting with initializing the directory with the init command and then a remote to point to your server.

$ git init
$ git remote add origin user@example-server.com:repos/project.git

With the remote added you can then add your files and push them to your server.

$ git add .
$ git commit -am 'initial commit'
$ git push -u origin master

Summary

Once configured you can now continue committing and pushing your changes to your server. Variations on the above introduction would include hosting the repos in a more public manager if you are working in a group and looking into something like Gitolite for managing your git hosting server.


Tags: git