Git setup

From Smith family
Jump to: navigation, search
Server setup
← Previous Next →
Scanner Samba

Git is a distributed version control system. I use Gitolite as a centralised git server. While the repositories reside on the server, administration is done by user@desktop on the desktop machine. Gitweb is used to give read-only access to some, but not all, repositories. Gitweb will only show repos that have an owner, desc, or category line in gitolite.conf; other repos will be hidden from Gitweb.

Install Git and Gitolite

Unfortunately, only Gitolite v2 is in the Ubuntu repositories, so I have to download Gitolite from source.

  • Install what you can from the Ubuntu repos
root@server:~# aptitude install git git-doc gitweb highlight
  • Create the git user:
root@server:~# adduser --disabled-password  git
root@server:~# su - git
git@server:~$ mkdir -p bin
git@server:~$ logout
(Logout then log back in again so that the .profile script adds ~/bin to the PATH environment variable.)
  • Add git to the list of users allowed to SSH into the server. Modify /etc/ssh/sshd_config so the AllowUsers line includes git:
# Only allow access by certain users
AllowUsers user1 user2 git
then restart sshd:
root@server:~# service ssh restart
  • Back on the desktop machine, create a public RSA key for user@desktop (the administrator user Gitolite):
user@desktop:~# ssh-keygen -t rsa
  • On the server, copy the key to the git user's home:
root@server:~# scp user@desktop:~/.ssh/ /home/git/
  • Download and install Gitolite
root@server:~# su - git
git@server:~$ git clone git://
git@server:~$ gitolite/install -to bin
git@server:~$ gitolite setup -pk
  • On the desktop machine, clone and modify the gitolite-admin repo as you see fit.
user@desktop:~$ git clone git@server:gitolite-admin
user@desktop:~$ cd gitolite

Install and setup git-daemon

Git-daemon allows low-resource anonymous cloning of repositories

  • Install git-daemon
root@server:~# aptitude install git-daemon-run
  • Modify /etc/sv/git-daemon/run, modifying the user group and the paths (the last two lines):
exec 2>&1
echo 'git-daemon starting.'
exec chpst -ugitdaemon:git \
  "$(git --exec-path)"/git-daemon --verbose \
  --base-path=/home/git/repositories/ /home/git/repositories/
  • Restart git-daemon:
root@server:~# sv restart git-daemon
  • For each repo you want accessible by git-daemon, add the R = daemon to the repo's config in gitolite.conf
repo testing
   RW+     =   @all
   R       =   daemon
then commit and push the changed admin file.

Setup gitweb

  • Modify the UMASK setting (to allow gitweb to read the repos) and the GIT_CONFIG_KEYS setting (to allow Gitolite to set gitweb descriptions) in /home/git/.gitolite.rc:
UMASK                       =>  0027,
GIT_CONFIG_KEYS             =>  'gitweb\..*',
  • Modify /etc/gitweb.conf to include:
$projectroot = "/home/git/repositories";
$projects_list = "/home/git/projects.list";

# Allow syntax highlighting                                                                                               
$feature{'highlight'}{'default'} = [1];
  • Add Apache's user to the git group:
root@server:~# usermod -a -G git www-data
  • Check the contents of /etc/apache2/conf-available/gitweb.conf:
Alias /gitweb /usr/share/gitweb

<Directory /usr/share/gitweb>
  Options +FollowSymLinks +ExecCGI
  AddHandler cgi-script .cgi
They should be OK without modification. There's no need to update /etc/apache2/sites-available/site.domain.tld.
  • Enable the gitweb config and reload Apache:
root@server:~# a2enconf gitweb
root@server:~# service apache2 reload
  • You should be able to view your repos from http://site.domain.tld/gitweb/

Gitweb in its own virtual host

Create the file /etc/apache2/sites-available/git.domain.tld:

<VirtualHost *:80>
       ServerAdmin webmaster@localhost

       DocumentRoot /usr/share/gitweb

       ServerName git.domain.tld
       ServerAlias git

       Alias /gitweb /usr/share/gitweb

       # Defined in /etc/apache2/conf.d/gitweb
       # <Directory /usr/share/gitweb>
       #   Options FollowSymLinks +ExecCGI
       #   AddHandler cgi-script .cgi
       # </Directory>

       RewriteEngine on

       # make the front page an internal rewrite to the gitweb script
       RewriteRule ^/$ /gitweb.cgi

       # make access for "dumb clients" work
       RewriteRule ^/(.*\.git/(?!/?(HEAD|info|objects|refs)).*)?$ \
               /gitweb.cgi%{REQUEST_URI}  [L,PT]

       # Possible values include: debug, info, notice, warn, error, crit,
       # alert, emerg.
       LogLevel warn

       CustomLog /var/log/apache2/git.domain.tld.access.log combined
       ErrorLog /var/log/apache2/error.log

       ServerSignature Off
  • Enable the site:
root@server:~# a2ensite git.domain.tld
root@server:~# service apache2 reload

Changing the default template

Modify the standard template if you want changes to affect all new repos created. Do this, for instance, if you want to use to automatically sign all commits by adding the file hooks/pre-commit:


# Run Keybase to sign the commit
keybase dir sign -p git
git add

Server side

Modify the contents of /usr/share/git-core/templates/ as you see fit, such as adding the commit hook above.

This should affect all repos created with Gitolite.

Client side

Copy /usr/share/git-core/templates/ to somewhere local, such as ~/.git_template.

Modify the contents as you see fit.

Tell Git to use this template rather than the default one:

user@desktop:~$ git config --global init.templatedir '~/.git_template'

This should affect all new repos created on the client, outside of Gitolote.

Import prior repos

  • Copy them to /home/git/repositories
  • Make them git-writable and gitweb-readable:
root@server:~# chown -R git:git /home/git/repositories
root@server:~# chmod -R g+rX ~/repositories/
  • On the desktop, modify gitolite-admin/conf/gitolite.conf
repo  repo1
   RW+ = user1
  • Manually add any existing gitweb descriptions to the desktop's gitolite-admin/conf/gitolite.conf file. (You can find the descriptions in /home/git/repositories/existing-repo.git/description
  • Commit and push the changes
 user@desktop:~/gitolite-admin$ git commit -a -m "Added imported repos"
 user@desktop:~/gitolite-admin$ git push

Common tasks

  • To clone a repo:
git clone git@git.domain.tld:repo.git
  • Create a new repo, first create its entry in gitolite-admin/conf/gitolite.conf and push the changes. This will create an empty repo on the server. Then clone it on the client machine, ignoring the message about cloning an empty repo.
  • To import a repo into Github, use the Github "import repository" functionality, using the url git://git.domain.tld/repo.git . Note that you may need to remove files larger than 100Mb, following Github's instructions.
  • To add Github as a remote for a repo,
git remote add github
git push github master
(Use this technique to import a repo on Github if the import doesn't work: create an empty repo on Github, add it as another remote for the existing repo, then push the changes to Github.)

See also

Gitolite 3 docs:

Installation walkthroughs: