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.

Install Rails 3 on Ubuntu

So the first thing you need when you decide to start rails development is a development environment.  I personally own a Macbook and while there seems to be this notion that rails developers all use Macs I still haven’t found a tactful way to run a purchase requisition through for one at work.  So being stuck with PC hardware the choice was Windows or Linux.

Windows actually can be used to develop rails applications and I did that for a week or two until I got the itch to put Linux to use.  Que about a week of lost productivity as I ran through all the distros looking for something I could use daily at work.  Surprisingly I was unable to find anything to replace Outlook and since it has become common for people to email me change requests or project ideas I really need a stable communication line with the Exchange server.  Yes, I could use the web client but when you’ve used Outlook for years you get used to being notified of new mail and having offline abilities available.

So without a replacement for Outlook easily available, I decided on the next best thing:  virtualization.  I have a license for VMware Workstation and it does have nice features like dual monitor support but all I need is a quick, light environment to work with.  Enter VirtualBox.  It’s open source and free to use so I can load it up on my computers at home or at work.  I picked XUbuntu for the distribution as it was light on resource usage and still has everything I need to feel at home.

With XUbuntu running on a fresh install the following steps will get you a rails development environment up and running in no time.  Since I compile ruby from source I need to install the development libraries and dependencies needed to do that.  The following is all one command:

apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libpq-dev

Once apt is done installing you can download the ruby source at http://www.ruby-lang.org/en/downloads/  You’ll want the latest 1.9 source which at this time is 1.9.2 p290.

Extract this to a folder and run the following command:

./configure && make && make install

Once ruby is installed you can check by running:

ruby -v

and it should display the version you downloaded.  Now we need to update the rubygems.

gem update --system

This will install the latest version of rubygems on the system.  Now we can begin to install the rails environment.  Run the following commands:

gem install rake

gem install rails

gem install pg

The last one will install the PostgreSQL library which is what I use, you can change it to whatever database you use.

After this I like to do a full update of all the gems.

gem update

This will update all the gems on the system but be aware that this WILL install new versions of rails or any other gems you have loaded if you run this command later.

You should now have a running rails environment ready for you to play with.