Setting up Gitweb on your Ubuntu workstation

2 Aug 2011

If you work in an IT environment, it’s nice to be able to quickly share some of your git repositories from your workstation, without setting up accounts and ssh keys ie using http.

Unfortunately, a lot of the posts out there on “how to setup Gitweb on Ubuntu” seem to make a meal of the whole process. I got it going after heading down a few dead-ends; here’s how I did it on Ubuntu 11.04 (Natty).

Basic Setup

Let’s start with a simple git repository:

sudo aptitude install git-all apache2
cd ; git init foo ; cd foo
echo "hello world" > file1.txt
git add . ; git commit -m "initial commit"

You probably only want to share out some of your repositories, not all of ${HOME}! So, do a bare clone of foo repository to /var/www:

cd /var/www
sudo git clone --bare ~/foo foo.git

Next, a little gotcha. You need to enable the post-update hook, so that the required info is generated for the http server. The “gotcha” is that you need to enable the hook then do a push to your server repository, otherwise the server info isn’t updated:

cd ~/foo/.git/hooks
mv post-update.sample post-update
git remote add web /var/www/foo.git
## do a test push
cd ~/foo
echo "test update" >> file1.txt
git add . ; git commit -m "test update"
sudo git push web

Now, if you browse to http://localhost you should see foo.git listed, and your workmates can now easily clone your work:

otherpc$ git clone http://yourpc/foo.git


The next step is to setup gitweb, so your workmates can easily browse your code, search for commits, etc. Gitweb is already installed as part of git-all, or you can do:

sudo aptitude install gitweb

The only change to make is to edit /etc/gitweb.conf to point to /var/www:

sudo vi /etc/gitweb.conf
## $projectroot = "/var/cache/git";
$projectroot = "/var/www";

And that’s it! Part of the gitweb install on Ubuntu adds in /etc/apache2/conf.d/gitweb, so there’s no other files to edit (unlike what blog1, blog2, or blog3 say).

Browse to http://localhost/gitweb/, and there’s your browse-able, search-able, git repository :-D

But wait, there’s more – automatically push your changes.

As you work, /var/www/foo.git is going to get out-of-date. You could remember to regularly push but that’s boring – automate it:

% cd ~/foo ; cat git.web.push
cd ${HOME}/foo
git remote add web /var/www/foo.git >& /dev/null
sudo git push -f web

.and automate it:

crontab -l
* * * * * ${HOME}/foo/git.web.push

And finally.

Make the layout pretty. Edit ~/foo/.git/description, add an index.html, a custom theme, some javascript, .


Brian (comments) has added some improvements to my instructions – see below. Thanks Brian!

comments powered by Disqus

  « Previous: Next: »