DIY git remote repository

I have a secret to tell you.  There was a long period of time where I didn’t use a source revision control system.  *GASP*  Honestly, I mostly work alone so it never seemed like the effort was worth the payout.  I made copies of my code before I did major changes and always had backups.  I did have a brief run-in with SVN and while the idea of having your files watched and backed up as needed was nice, it turned into a hassle when I would move between machines or work offline.

Then I started rails development and git was thrown at me as a defacto standard.  At first I thought of looking for a GUI like I had with SVN but since I move between operating systems pretty regularly depending on where I am, I wanted something I could count on being where I was no matter what.  The command line utility was my choice.  It’s really not too difficult once you get used to it and even now I really don’t use it to it’s full potential as it’s mostly just myself.  But if I want to add a developer or a whole team to a project I can and we can all work seamlessly.

Once you decide to use git you need to have a place to upload your source.  A lot of people will use github and if you have an open source project that’s a great idea because it’s free.  For me, I already had a VPS and wanted to utilize that instead of paying another monthly fee.  Here’s how I got it working on my server.

Just a quick note about the server, my VPS is a Linux server.  I cannot say if this would work on a Windows server but as long as you can get a SSH server and git working there should be no reason it wouldn’t.  I suggest using Linux.

First you create a git user.  Doesn’t matter what you call it, but this will be the user you will use to log in to your server.  I suggest creating a separate user so you can store all your repositories in a fresh home directory.  Then log in to that user and do the following:

To create a new project repository on the server:

First create a directory to contain the code

mkdir myrepo.git

Then change into that directory and create a bare git repository

git --bare init

That’s it!  Now on your development machine we will setup git to push to this server.

I suggest setting up your name and email in git if you haven’t already done so.  You can do that using the following commands:

git config --global user.name "Your full name"

git config --global user.email "your@email.com"

In your project directory do the following:

Initialize the repository

git init

Add all the files to the repository

git add .

Commit the files to the repository

git commit -m "Initial commit"

Now setup the remote repository

git remote add origin gituser@yourserver.com:myrepo.git

[ Replace gituser with your user, yourserver.com with your domain and myrepo.git with your repository directory you create on your server ]

Now push the code to the server

git push origin master

It should ask you for the password for the user you used.  Once you enter it your code will be pushed into the repository.  If everything was setup correctly you should now be able to utilize your own server for housing your source code.

To pull down your code from your server you do the following in your projects folder:

Clone the repository

git clone gituser@yourserver.com:myrepo.git

This will prompt you for your password and then once entered will create the project directory and pull your code down from the server.  This is a new repository ready to go and you can push and pull on it as you need.

There are ways to get SSH to store your password on your development machine so you don’t have to keep typing in your password but I don’t use that mechanism.  For me, I just wanted to use my server to store my files between sites.  I’m sure there are better ways to do it, but this has worked for me for a while and I wanted to document it in case it was helpful to anyone else just looking for a quick and dirty DIY git repository server.