Git Server Configuration

From Thomas-Krenn-Wiki
Jump to navigation Jump to search

The following article will explain how to install and configure a Git-Server for managing a repository (or repo). For this, there will be a public Git repository, which can be "cloned" (git clone) through HyperText Transfer Protocol (HTTP) or browsed using gitweb. Since this repo can only be read, it serves purely for providing the users access to the current version of the application. However, developers can push the active changes from their local working copies into the public repo, assuming that they have Secure SHell access on the server. Thereby, the users get any potentially available updates. The examples and configurations shown in this article have been performed on a Debian Squeeze Server.

Installation

The packages, git and gitweb will be installed on the server in the first step.

apt-get install git gitweb

If a web server does not exist then Apache will be automatically installed during the gitweb installation.

Setting up the Public Repo

The following configuration steps are necessary for releasing a repository through HTTP with the ability to access it via gitweb. A directory, where the repo will be located, will be created in the first step. A bare repo will be created, since this should involve a public repo.

:~/repos$ mkdir repos/pub
:~/repos$ mkdir repos/pub/stable
:~/repos$ cd pub/stable/
:~/repos/pub/stable$ git --bare init
Initialized empty Git repository in /home/tktest/repos/pub/stable/
:~/repos/pub/stable$ ls
branches  config  description  HEAD  hooks  info  objects  refs

As can be seen in the output of the ls command, a bare repo is not comparable with a conventional working copy. The files that are normally located in the .git folder can be viewed directly. However, the "normal" files (which will be edited) are not located directly in the repo, but can be accessed through the managed history of the repo. Bare repos are specially intended for publishing repositories, since the last version of the repo can be cloned and pushed into the repo without danger through them, because there a working copy does not exist.[1][2]

Publication through HTTP

In the next step, the Apache web server will be configured so that the public repository directory will be accessible. Furthermore, everyone who wants to work with the current version of the repo can fetch it to their own machine using the "git clone" command. A custom Vhost will be first be created for Apache (root right will be required for configuring Apache)[3].

:~# vi /etc/apache2/sites-available/stable

Access to the repository directory will be controlled in this file. Since no host names have been configured on the test server in use, different ports will be used for various services. The public repo can be accessed through Port 81, afterwards.

Listen 81
<VirtualHost *:81>
    Alias /stable "/home/tktest/repos/pub/stable"
    <Location stable>
        Allow from all
    </Location>
</VirtualHost>
Directory listing for the "Stable" public repo.

Afterwards, the created web site will be activated, and the Apache configuration will be reloaded.

:~# a2ensite stable
Enabling site stable.
Run '/etc/init.d/apache2 reload' to activate new configuration!
:~# /etc/init.d/apache2 reload

After these configuration steps, the repo will be accessible from a web browser through http://SERVER-IP:81/stable (the SERVER-IP must be replaced with the corresponding IP address configured for the web server). A simply directory listing will appear in the web browser (cp. figure), since gitweb has not yet been configured. However, before this directory can be cloned through Git, the git update-server-info command must be executed, which will update the informational files for the repository (in the "/home/tktest/repos/pub/stable" repository directory on the server):

git update-server-info

To avoid having to perform this update manually, there are so-called hook scripts. These scripts are automatically executed when certain events occur. Git provides several example scripts, which will be enabled once they have been renamed, in the ".git/hooks" folder.

:~/repos/pub/stable$ cd hooks/
:~/repos/pub/stable/hooks$ mv post-update.sample post-update

Once it has been enabled, the update command will automatically be executed each time new changes are pushed into the repo.

Gitweb Configuration

Gitweb provides a comfortable web interface for listing and searching Git repos. To make the stable repo on the server available, the central configuration file:

vi /etc/gitweb.conf

would be modified as follows:

# path to git projects (<project>.git)
$projectroot = "/home/tktest/repos/pub";

In turn, a customized virtual host will then be crated for the Apache server.

vi /etc/apache2/sites-available/gitweb

Gitweb’s access to the repo will then be confirgued in this file.

Listen 82
<VirtualHost *:82>
        DocumentRoot /var/cache/git
        SetEnv GITWEB_CONFIG /etc/gitweb.conf
        Alias /gitweb.css /usr/share/gitweb/gitweb.css
        Alias /gitweb.js /usr/share/gitweb/gitweb.js
        Alias /git-favicon.png /usr/share/gitweb/git-favicon.png
        Alias /git-logo.png /usr/share/gitweb/git-logo.png
        ScriptAlias /gitweb.cgi /usr/lib/cgi-bin/gitweb.cgi
        DirectoryIndex gitweb.cgi
</VirtualHost>

The web page can be re-activated in the next step and the Apache configuration re-loaded.

:~# a2ensite gitweb
:~# /etc/init.d/apache2 reload

The Gitweb interface will appear once Port 82 on the web server has been called.

Gitweb.png

Git through Secure SHell

The simplest path to access the server from the client for writing is through Secure SHell (SSH). If SSH access has been provided on the server, changes can be pushed directly.

:~/Repos/stable$ touch erste_datei_im_repo
:~/Repos/stable$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	First_file_in_repo
nothing added to commit but untracked files present (use "git add" to track)
:~/Repos/stable$ git add first_file_in_repo
:~/Repos/stable$ git commit -m "First file in repo"
[master (root-commit) 4500243] Erste Datei ins Repo
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 erste_datei_im_repo
:~/Repos/stable$ git status
# On branch master
nothing to commit (working directory clean)
:~/Repos/stable$ git push ssh://tktest@SERVER-IP/home/tktest/repos/pub/stable master
tktest@SERVER-IP's password: 
Counting objects: 3, done.
Writing objects: 100% (3/3), 232 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To ssh://tktest@SERVER-IP/home/tktest/repos/pub/stable
 * [new branch]      master -> master

Using the "git push" command, the changes are pushed from the repo on the client into the public repo on the server. After that, the Gitweb display should update the content of the stable repo and no longer be empty. The meaning of the commands issued will be explained in more details in the Git Basic Commands article.

References

Related articles

Git Basic Commands
Git-annex Archive with Git-annex Assistant
Git-annex detailed information