View Your Repositories Remotely with GitWeb

If you've been following my blog, you probably saw the post where I outlined my personal git server setup. In it, I showed off the ls command that I've configured for quickly viewing my repositories remotely. This is quite limited though, in that it only shows me the names of the repositories, without any further details or even being able to see the files within the repositories unless I clone them locally. Luckily, there's a much better way to view your repositories remotely, via GitWeb. Better yet, getting up and running is super simple on Ubuntu. In my use case, I'm using Apache, so the first step is to install both packages.

# apt install apache2 gitweb

With the packages installed, we'll need to enable CGI scripts, which are what power GitWeb

# a2enmod cgi

Next, we'll need to create a virtualhost config for the (sub)domain.

# cat <<EOF > /etc/apache2/sites-available/gitweb.conf
<VirtualHost *:80>
    DocumentRoot /usr/share/gitweb
    <Directory /usr/shar/gitweb>
        Options +ExecCGI +FollowSymLinks +SymLinksIfOwnerMatch
        AllowOverride All
        order allow,deny
        Allow from all
        AddHandler cgi-script cgi
        DirectoryIndex gitweb.cgi

By default, gitweb is going to look for your git repos in /var/lib/git, so if you've got your repos stored elsewhere, you'll need to open up /etc/gitweb.conf and set the correct value for the $projectroot variable there. Once that's all ready to go, make sure you enable the site...

# a2ensite gitweb

… and start up Apache.

# systemctl start apache2

You can then browse to the domain you've configured and you should see a listing of your repos, along with some other helpful information like who was the last person to push a commit, and when.

Bonus: Restricting the Site to Authorized Users

If you don't want to open up the site to the public, you can restrict it with HTTP Basic Auth. To do so, replace the following lines in the /etc/apache2/sites-available/gitweb.conf file:

order allow,deny
Allow from all

with the following lines:

Order deny,allow
AuthType Basic
AuthName "Restricted Access"
AuthBasicProvider file
AuthUserFile "/path/to/.htpasswd"
Require valid-user

Don't forget to set up the htpasswd file though:

# htpasswd -c /path/to/.htpasswd your-user

And that's it! Feel free to reach out if you have any tips on how to make this any better, I'm always on the lookout for new tips and tricks!