Sunday, May 15, 2011

Setting up a GIT server on Ubuntu

I have all my code in a GIT repository on my home machine.
Everytime I wanted to deploy the code for my pet project site I used to fire up winscp and copy the code manually to the server. Well, as anyone would guess this was not only inefficient but error prone. Also reverting back to an old version just in case I ran into issues used to be a series of deletes & copies of directories.

GIT to rescue
The "divineness" (you kidding me ! this is not a word !!), anyways, let me start again. The "divineness" in me finally made me create a git server on my hosting server (slicehost). BTW, 2 words for my hosting company(its long due), I wanted to have a linux server with full root access and do whatever I wanted. Maximum flexibility and control. Plus I wanted 24x7 support. Slicehost has both, their customer service is awesome !! I always log into their chat and ask them stuff and they go above and beyond everytime.

K, no further digressions, lets get dirty

Setting up you git server on Ubuntu
The first thing you need to do is to install git on the Ubuntu server. That's a breeze:
apt-get install git-core

Now lets setup a directory where git server will run out of -
mkdir /opt/gitrepo
cd /opt/gitrepo

Initialize git :
git init --bare
The --bare flag tells git that this is a brand new directory where git will be setup

Pushing to this repository from your home machine.
For the purpose of this post, lets assume I have my git running at c:\myrepo also I'm writing this from a windows machine, so I'm in my gitbash

k, the first thing is telling your machine the server location.
git remote add origin ssh://planet@mydomainname.com/opt/gitrepo
Lets go over the command :
git remote add origin : I'm adding the origin of this repository as
ssh://planet@mydomainname.com/opt/gitrepo : 
ssh is the protocol
planet : this is the user name
@mydomainname.com : Well this is the name of your slice where you are setting the git server
/opt/gitrepo : The location of the server repo base we setup earlier

Push it to the server
Now lets push the master branch on your home machine to the server :
git push origin master
** master = This is the master branch on the home machine.
If you want to push some other branch, say "currentWorkingBranch", then type :
git push origin currentWorkingBranch

Once you run the command you'll see compression of files and pushing them off to the server, the output will end with something like :
* [new branch]      master -> master
OR
* [new branch]      currentWorkingBranch -> currentWorkingBranch

Verify on the server
On your server, go to /opt/gitrepo and type :
git branch
and you should see the branches you have pushed till now.

Remember, if you list the directories you WON'T see your code files.
Again, you WON'T see your code files.
Why ? - Because this is a server. You files are present internally and there is no working branch, hence you will get an error if you try this :
git status

Now you'll want to extract data out of this server - Read clone
So lets do that.
For testing create /temp/temprepo
in that just type :
git clone /opt/gitrepo/ /tmp/temprepo/
Now list the files and you'll see your code in /tmp/temprepo

btw, if you want to checkout a particular branch from the server, then first create a branch where you want to have the data :
e.g.
git branch branch-April2011 origin/branch-April2011
now checkout this branch :
git checkout branch-April2011
That's it you should have cloned the data only for that repository

The irc channel #git on freenode is very helpful, I'd suggest checking it out if you get stuck.
Ofcourse you can ask me about this.

No comments:

Post a Comment