Git setup

From Smith family

Jump to: navigation, search
Server setup
← Previous Next →
Subversion Samba

Git is a distributed version control system.

Contents

Set up a basic Git repository home

This is the place where your externally-accessbile Git repositories will reside.

  • Install the Git packages:
root@server:~# apt-get install git-core gitweb git-svn fakeroot python-setuptools
  • Create the git user:
root@server:~# adduser --system --shell /bin/sh --gecos 'git version control' --group --disabled-password --home /home/git git
  • Create somewhere to keep the repositories:
root@server:~# sudo -u git mkdir /home/git/repositories
  • Allow SSH logins from trusted users. For each user who wants to log in, add their SSH key to the git user's authorized_keys file. (See Password-less SSH logins for details)
  • Allow SSH sessions to the git user. Edit /etc/ssh/sshd_config and add git to the AllowUsers line:
AllowUsers user1 user2 git
  • Check that you can SSH into this user
user@desktop:~$ ssh git@server
(you may need to restart the SSH daemon for it to pick up the new user)

Set up Gitosis

  • Install the python setup tools needed for Gitosis
root@server:~# apt-get install python-setuptools
  • Download and install Gitosis on the server:
root@server:/home/git# sudo -u git git clone git://eagain.net/gitosis.git
root@server:/home/git# cd gitosis
root@server:/home/git/gitosis# python setup.py install
  • Copy the desktop user's public SSH key to the server and tell Gitosis about it:
user@desktop:~$ scp ~/.ssh/id_rsa.pub user@server:/tmp/id_rsa.pub 
root@server:/home/git/gitosis# sudo -H -u git gitosis-init < /tmp/id_rsa.pub
  • Remove the original key from /home/git/.ssh/authorized_keys (the one without the 'autogenerated by gitosis' line)
  • Ensure that the gitosis hooks are executable:
root@server:/home/git# sudo chmod 755 /home/git/repositories/gitosis-admin.git/hooks/post-update
  • On the desktop machine, checkout the Gitosis repository
user@desktop:~$ git clone git@server:gitosis-admin.git
user@desktop:~$ cd gitosis-admin
  • Update the gitosis.conf file as you wish (see below) then push the changes back to Gitosis
user@desktop:~/gitosis-admin$ git commit -a -m "Some commit message"
user@desktop:~/gitosis-admin$ git push

Set up git-daemon

Git-daemon is a simple tool that allows people to retrieve git repositories using the git:// protocol. Unfortunately, the standard Ubuntu init script seems to have a bug, but it's easy enough to roll your own.

  • Create the file /etc/init.d/git-daemon:
#!/bin/sh

NAME=git-daemon
PIDFILE=/var/run/$NAME.pid
DESC="the git daemon"
DAEMON=/usr/lib/git-core/git-daemon
DAEMON_OPTS="--base-path=/home/git/repositories --export-all --verbose --syslog --detach --pid-file=$PIDFILE --user=git --group=nogroup"

test -x $DAEMON || exit 0 

[ -r /etc/default/git-daemon ] && . /etc/default/git-daemon

. /lib/lsb/init-functions

start_git() {
  start-stop-daemon --start --quiet --pidfile $PIDFILE \
    --startas $DAEMON -- $DAEMON_OPTS
}

stop_git() {
  start-stop-daemon --stop --quiet --pidfile $PIDFILE
  rm -f $PIDFILE
}

status_git() {
  start-stop-daemon --stop --test --quiet --pidfile $PIDFILE >/dev/null 2>&1
}

case "$1" in
 start)  log_begin_msg "Starting $DESC"
         start_git
         log_end_msg 0
         ;;
 stop)   log_begin_msg "Stopping $DESC"
         stop_git
         log_end_msg 0
         ;;
 status) log_begin_msg "Testing $DESC: "
         if status_git
         then
           log_success_msg "Running"
           exit 0
         else
           log_failure_msg "Not running"
           exit 1
         fi
         ;;
 restart|force-reload) log_begin_msg "Restarting $DESC"
         stop_git
         sleep 1
         start_git
         log_end_msg 0
         ;;
 *)      echo "Usage: $0 {start|stop|restart|force-reload|status}" >&2
         exit 1
         ;;
esac
exit 0
  • Make it executable
root@server:~# chomd a+x /etc/init.d/git-daemon
  • Make it a service and start it:
root@server:~# update-rc.d git-daemon defaults
root@server:~# /etc/init.d/git-daemon start
  • Don't forget to allow the git port (9418) through the firewall

Set up gitweb

We'll keep all the Gitweb files under /home/git/gitweb. The gitweb.cgi script will reside in /var/www/cgi.site.domain.tld/, to keep all the web files under /var/www.

  • Copy gitweb.cgi to the right place:
root@server:~# cp /usr/lib/cgi-bin/gitweb.cgi /var/www/cgi-bin.site.domain.tld/
  • Edit /etc/gitweb.conf:
$my_uri = "http://site.domain.tld/gitweb";
$site_name = "site.domain.tld/gitweb";

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

# directory to use for temp files
$git_temp = "/tmp";

# target of the home link on top of all pages
$home_link = $my_uri;

# html text to include at home page
$home_text = "indextext.html";

# file with project list; by default, simply scan the projectroot dir.
$projects_list = $projectroot;

# stylesheet to use
$stylesheet = "/gitweb/gitweb.css";

# logo to use
$logo = "/gitweb/git-logo.png";

# the 'favicon'
$favicon = "/gitweb/git-favicon.png";
  • Edit /etc/apache2/sites-available/site.domain.tld to include this section as part of the VirtualHost section:
RewriteEngine on
RewriteRule ^/gitweb/([a-zA-Z0-9_\-]+\.git)/?(\?.*)?$ /cgi-bin/gitweb.cgi/$1 [L,PT]

Alias /gitweb /home/git/gitweb
<Directory /home/git/gitweb>
  Options Indexes FollowSymlinks ExecCGI
  DirectoryIndex /cgi-bin/gitweb.cgi
  AllowOverride None
</Directory>
<Directory /home/git>
  Order allow,deny
  Allow from all
  AllowOverride None
</Directory>
  • Copy the .css and other files to /home/git/gitweb and fix permissions:
root@server:~# cp /usr/share/gitweb/* /home/git/gitweb/
root@server:~# chown -R git:git /home/git/gitweb
  • Ensure existing repositories are accessbible to the www-data user
root@server:~# chmod a+rx /home/git/repositories/repo1.git
  • Reload the Apache configuration
root@server:~# /etc/init.d/apache2 reload

You should now be able to browse to http://site.domain.tld/gitweb and see a list of publically-available repositories.

Creating new repositories in gitosis

Perform these steps on the client machine.

  • If needed, create a group of users by adding a [group] section to gitosis.conf
  • Add the repository name to the writable line in the group.
  • Push the updated gitosis.conf back to gitosis:
user@desktop:~/gitosis-admin$ git commit -a -m "Added new repository repo1"
user@desktop:~/gitosis-admin$ git push
You will get an error
remote: WARNING:gitosis.gitweb.set_descriptions:Cannot find 'repo_new' in '/home/git/repositories'
remote: WARNING:gitosis.gitweb.generate_projects_list:Cannot find 'repo_new' in '/home/git/repositories'
This is normal.
  • Create the new repository:
user@desktop:~$ cd repo1
user@desktop:~/repo1$ git init
user@desktop:~/repo1$ git add .
user@desktop:~/repo1$ git remote add origin git@server:repo1.git
<add some files>
user@desktop:~/repo1$ git push origin master:refs/heads/master
  • You will now be able to add a [repo] section to gitosis.conf and push it.

Once you've put the repository on the server, you'll probably need to make it world-readable so that gitweb can pick it up

root@server:~# chmod a+x /home/git/repositories/repo1.git

Creating new users in gitosis

Perform these steps on the client (desktop) machine.

  • Get the public SSH keys from the users you want to add (e.g. alice.pub and bob.pub). Note that the key filenames must have the .pub extension
  • Copy the files into the keydir directory of your local copy of the gitosis repository
user@desktop:~$ cd /path/to/gitosis-admin
user@desktop:~/gitosis-admin$ cp ~/alice.pub keydir/
user@desktop:~/gitosis-admin$ cp ~/bob.pub keydir/
user@desktop:~/gitosis-admin$ git add keydir/alice.pub keydir/bob.pub
  • Update the relevant members line in the gitosis.conf file:
[group myteam]
members = jdoe alice bob
writable = repo1
  • Commit and push the changes:
user@desktop:~/gitosis-admin$ git commit -a -m "Granted Alice and Bob commit rights to Repo1"
user@desktop:~/gitosis-admin$ git push
  • Alice and Bob can now clone the repo1 repository like so:
git clone git@YOUR_SERVER_HOSTNAME:free_monkey.git
They also have commit rights.

Converting a Subversion repository to Git

The main problem is converting the Subversion branches and tags into Git branches.

Create a clone of the SVN repository:

user@desktop:~$ git svn clone -A authors.txt --trunk=path/to/trunk --branches=path/to/branches --tags=path/to/tags --no-metadata --username=user1 http://site.domain.tld/svn/svn_repo
user@desktop:~$ cd svn_repo
  • Convert all the remote branches to local ones
user@desktop:~/svn_repo$ git fetch . refs/remotes/*:refs/heads/*
  • Remove all the remote branches
user@desktop:~/svn_repo$ for branch in `git branch -r`; do git branch -rd $branch; done
user@desktop:~/svn_repo$ git branch -d trunk
  • Remove the SVN records
user@desktop:~/svn_repo$ git config --remove-section svn-remote.svn
user@desktop:~/svn_repo$ rm -rf .git/svn/
  • Convert the 'tag' branches to proper tags
user@desktop:~/svn_repo$ git-for-each-ref refs/heads/tags | cut -d / -f 4 | while read ref ; do git tag -a "$ref" -m "Tagging $tag" "refs/heads/tags/$ref" ; git branch -D "tags/$ref" ;  done  
  • Upload the repository to the git server:
user@desktop:~/svn_repo$ git remote add origin git@git.domain.tld:repo.git
user@desktop:~/svn_repo$ git push origin master

Sample gitosis.conf file

Here's a sample file

[gitosis]
# By default, gitweb and git-daemon won't process repositories
gitweb = no
daemon = no

[group gitosis-admin]
writable = gitosis-admin
members = user@domain.tld

[group groupone]
members = user@domain.tld
writable = repo1 repo2

[repo repo2]
gitweb = yes
daemon = yes
description = A description of the repository

See also

Personal tools