Well I know there are a lot of tutorials on the internet regarding the installation of Git server on Ubuntu but I thought I’d try to ‘KISS’ (Keep it simple stupid!), most of the tutorials are full of non-nessacery information and quite often don’t go into detail (or at least explain in an easy to follow way) about why we set it up this particular way.
In this tutorial I plan to explain in an easy to follow way a proven method of installing Git to host Git repositories on a Ubuntu Server.
Installing Git
This needs to be done on the server and also on your workstation, these instructions however need to be run on the server logged in as ‘root’ user – or use ‘sudo’ but I do it under ‘root’ personally.
sudo apt-get install git-core
Installing Gitosis
Gitosis seems to be the ‘trendy’ way for easy configuration of Git servers, so we’ll install that now too.. So we’re going to use Gitosis which needs python and a python setup tool to get running. Grabbing the python setup tool in Ubuntu will grab all the requirements if you don’t already have them:
sudo apt-get install python-setuptools
Now that we have python and the python setup tools installed we need to ‘clone’ the gitosis software (as that is the standard way to grab it, we are grabbing it from a git repository :)) – So what we are doing now is creating a temporary folder called ‘src’ of which we will be using to store the gitosis code.
mkdir ~/src
cd ~/src
git clone git://github.com/res0nat0r/gitosis.git
Now what we do is install it (Gitosis) using the python setup tool we grabbed earlier.
cd gitosis
sudo python setup.py install
Setting security…
We’re next going to add a new user called git. This is the guy that will do all the heavy lifting for us!
sudo adduser –system –shell /bin/sh –gecos ‘git version control –group –disabled-password –home /home/git
git
IMPORTANT: If you have locked down ssh, don’t forget to go into your /etc/ssh/ssh_config file and add git to the list of Allowed Users that can login. That list of users is separated by a space not a comma.
I always encourage the use of public/private key exchange and in the case of gitosis it looks to be required. Generate a key if you haven’t already. See instructions for public/private key here. I’m going to assume that from this point on you can access your server from your local machine using a public key exchange!
Next, copy your id_rsa.pub file over to your server somewhere (in our example we’re using /tmp) and then run this command:
sudo -H -u git gitosis-init < /tmp/id_rsa.pub
You’ll see output like this:
Initialized empty Git repository in ./
Initialized empty Git repository in ./
We’re now done with the server config; from your local machine, test it out with this:
git clone git@YOUR_SERVER:gitosis-admin.git
If all went well you have a gitosis-admin directory with a gitosis.conf file and keydir directory. We’re basically setup now. We just need to create a new repository and push it to the server.
Config Gitosis for a new project (repository)
Use your favorite editor to create a new block under the gitosis one. It should look like this:
[group myproject]
members = ballen@Bobs-iMac.local
writable = myproject
A couple of things to watch out in the above block. First, make sure your name matches what’s in your public key (that is, open your id_rsa.pub file and see that what the name says. Mine says ballen@Bobs-iMac.local so that’s what I have above. Yours will be different.) Second, make sure you spell writable correctly!
Once you’re done, commit and push the changes up to the server.
git commit -a -m “created a new repository!”
git push
What we’ve basically done in this step is configured gitosis to accept a new repository. We then submitted that new configuration to the server using Git itself. Genius!
Put your local code under version control.
Now that the myproject project is waiting for us on the server let’s go put it under version control on our local machine.
cd myproject
git init
As you may have heard, most of the goodness with git is in a special hidden .git directory at the root of your project. That’s pretty cool since it means removing your project from version control is as simple as erasing that directory. Way easier than subversion.. especially if something goes wrong..
If you’re a Rails developer you may want to blacklist some things from being under version control. Open up a text editor and create a file called .gitignore in the root of your project directory. Fill it up with this goodness:
.DS_Store
log/*.log
tmp/**/*
config/database.yml
db/*.sqlite3
Designate the server as a remote repository.
An interim step here is to remote add the files to the server we set up previously. Honestly I’m not sure what’s going on here under the covers but it’s necessary..
git remote add origin git@YOUR_SERVER:myproject.git
Add files and commit!
Let’s add everything to git, (note the trailing dot) and then push your initial commit to the server.
git add .
git commit -a -m “initial import”
git push origin master:refs/heads/master
Once that’s done you’re done! You can grab your code from any place that has public key access to your server using this command. – You’ll need to ensure you add the contents of the public key to the /etc/authorized_hosts file to allow access to the Git server (Don’t add any extra lines etc.).
git clone git@YOUR_SERVER:myproject.git
Adding additional repositories are as easy as adding another block to the gitosis-admin.conf file and then initialising it using the above method.
Hope you enjoyed this tutorial, much of the content came from this forum post, I have tested and ensured that it is working perfectly in Ubuntu Server 11.04 and added extra information where I have felt it is necessary to explain certain things.