Skip to content

Git Local Repository Setup Guide

Rami Al-Ghanmi edited this page Oct 24, 2013 · 15 revisions

The goal of this guide is to show you how to setup a local git repository with a web interface on your local machine. The process involved creating a shared git user account to access the repository. The account is shared in the sense that all repository users will use it to push/pull from the server, but git is setup to give each user credit for their work.

Repository Setup & Essentials

  1. First, we create the git user and set the account up for SSH Public Key Authentication and no terminal login. That means, the git account can not login using a password (only via PKA) and has no regular shell access. Instead, it will be using a special git shell with a limited set of commands
#Create git user account
sudo adduser --shell $(which git-shell) --gecos 'git version control' --disabled-password git

#Add git user to the appropriate groups
sudo usermod -a -G www-data git
sudo usermod -a -G developers git

#Setup authorized_keys file for access
sudo mkdir -p /home/git/.ssh
sudo touch /home/git/.ssh/authorized_keys
sudo chmod 600 /home/git/.ssh/authorized_keys
sudo chmod 700 /home/git/.ssh

#Copy the git-shell-commands to get limited shell access
sudo cp -r /usr/share/doc/git/contrib/git-shell-commands /home/git/
sudo chmod 750 /home/git/git-shell-commands/*

#Fix permissions
sudo chown -R git:git /home/git/
  1. Add your SSH key to the authorized key list. You can repeat this step for all users you wish to give access to. If you have yet to generate a key-pair, follow steps 1-3 in this article
cat ~/.ssh/id_rsa.pub | sudo tee -a /home/git/.ssh/authorized_keys
  1. Allow the git user to access the system via SSH
echo "AllowUsers git" | sudo tee -a /etc/ssh/sshd_config
sudo service ssh restart
  1. Create a location to store repositories
sudo mkdir -p /home/repo
sudo chown -R git:www-data /home/repo

GitWeb Setup

  1. Install gitweb
sudo apt-get install gitweb
  1. Update the gitweb configuration file at /etc/gitweb.conf
sudo cp /etc/gitweb.conf /etc/gitweb.conf.$(date +%Y-%m-%d)
sudo sed -i 's/^\$projectroot.*/\$projectroot = \\"\\/home\\/repo\\";/' /etc/gitweb.conf
echo -e "\n\n# User additions" | sudo tee -a /etc/gitweb.conf
echo "\\$feature{'blame'}{'default'} = [1];" | sudo tee -a /etc/gitweb.conf
echo "\\$feature{'search'}{'default'} = [1];" | sudo tee -a /etc/gitweb.conf
echo "\\$feature{'highlight'}{'default'} = [1];" | sudo tee -a /etc/gitweb.conf
echo "\\$feature{'grep'}{'default'} = [1];" | sudo tee -a /etc/gitweb.conf
  1. Setup GitWeb executable in /home/git and use Kogakure's gitweb-theme
sudo mkdir /home/git/gitweb
sudo ln -s /usr/share/gitweb/gitweb.cgi /home/git/gitweb/index.cgi
git clone https://github.com/kogakure/gitweb-theme.git
sudo mv gitweb-theme /home/git/gitweb/static
sudo chown -R git:www-data /home/git/gitweb
  1. Edit Lighttpd configuration for localhost to include:
# GitWeb
$HTTP["url"] =~ "^/gitweb" {
	server.document-root = "/home/git/"
	server.indexfiles = ("index.cgi")
	cgi.assign = (".cgi" => "")
}

Create a HelloWorld Repository

Now that the infrastructure is setup, lets create a HelloWorld repository to test out the configuration.

  1. Create a bare repository
#Create the directory (always end with .git)
sudo mkdir /home/repo/helloworld.git
cd /home/repo/helloworld.git
#Initialize a bare repository
sudo git --bare init

#Some meta-data
echo "Hello World Repository. Testing system configuration" | sudo tee /home/repo/helloworld.git/description
echo "[gitweb]" | sudo tee -a /home/repo/helloworld.git/config
echo -e "\towner = \\"Rami Al-Ghanmi\\"" | sudo tee -a /home/repo/helloworld.git/config

#Fix ownership of repository
sudo chown -R git:www-data /home/repo/helloworld.git
  1. Clone the repository, though empty, and add some code. Ignore the warning about cloning an empty repository.
git clone git@$(hostname):/home/repo/helloworld.git
cd helloworld
wget https://raw.github.com/gist/3205222/HelloWorld.cpp
git add HelloWorld.cpp
git commit -m "Initial commit with HelloWorld in C++"
git push origin master
  1. Now that we know the repository works, lets check the web interface. The URL should be: http://localhost/gitweb and you should see the helloworld repository. To check the code-base, follow the tree link.

In conclusion, for any new repository you want to create follow the steps you did in Create a HelloWorld Repository.