Run Your Own Personal Git Repo On a VPS
Posted by postfuturist on 2010-08-08 19:14:44

If you've got a VPS and you'd like to use it to host private git repositories, as a backup for your own work, it's pretty simple. First thing is you need to setup password-less ssh logins. That's covered in a lot of places, but I'll get to that too, if need be. From your own Linux machine you need to create a new keypair if you haven't already. Run this command:

$ ssh-keygen -t rsa

It may ask you for a passphrase, this is so you can protect the key itself on your personal computer. It's not necessary, but if your Linux distro is Ubuntu, it will only force you to enter that once per login to use the key. Just leave it empty if you aren't sure. Then you want to copy the public key to your server:

$ ssh-copy-id -i ~/ssh/id_rsa.pub username@yourserver.com

It will ask for your ssh password during this step. After you run this you'll want to test it out and see if you can log in to your ssh account without a password:

$ ssh username@yourserver.com

Assuming that is working, you can simplify logging into the server even further by creating a ~/.ssh/config file that looks like this:

host yourserver.com
user username
port 12345

You don't need the port setting if you are using port 22 or the user setting if the username is the same as the account on your machine. Having the host setting allows you to tab complete when ssh'ing from the terminal which is handy. Now for the good part, ssh into your server if you haven't already. Create a folder with a ".git" extension and initialize it:

$ mkdir myproject.git
$ cd myproject.git
$ git init --bare .

Log out of your server so you're back in the local terminal and clone the repository you just created:

$ git clone git+ssh://yourserver.com/home/username/myproject.git

That will create an empty folder called "myproject". Put some files in there, commit a revision and push that revision to the master server:

$ cd myproject
$ cp ../somefile.txt .
$ git add .
$ git commit -m "initial commit"
$ git push origin master

From now on, all you have to do to push local commits is "git push". Enjoy.


blog comments powered by Disqus