Hosting Git Repositories

From ParabolaWiki
Jump to: navigation, search

1 Hosting Git Repositories

Git is an excellent tool for distributed development, though it's not always used in a distributed fashion. This guide will help you setup the git daemon that serves the git:// protocol on your machine, so you can host your repos and share them with other developers without having to use an intermmediate and centralized git server.

1.1 Packages needed

Install git on your system. If you're going to use it on a LAN, avahi can serve local address using mDNS (yourhostname.local). If you want to serve git to the Internet (or a Free Network!) you'll need to point a DNS name to your machine, and probably open and forward the git-daemon port to your host on your router.

The git-daemon port is 9418.

1.2 Create the environment

You'll need a git user and group, and the /srv/git directory to put your repositories on. Add the git group and user, and add your regular user on the git repo.

# groupadd --system git
# useradd --home /srv/git --create-home --gid git --system --shell /bin/false

The above commands will create git as a system user that can't be used to login.

# gpasswd -a you git
# chmod 775 /srv/git

Add yourself to the git group. Repeat if you have several users. Then make /srv/git writable by the git group.

1.3 Creating repositories

To create a repository you have to create a directory with the ".git" suffix under the /srv/git dir. The suffix is not needed but it's custom to do so.

The we start a bare bones git repo in it. This means there will be no working tree (the tracked files) in it, thus marking it simply as an intermmediary repo between your working copy (anywhere in your ~) and the rest of the world. It also occupies less disk space.

$ mkdir repository.git
$ cd repository.git
$ git init --bare

If you already have a working copy somewhere else, we just add this repository as a remote (but local) copy and then we push to it.

$ cd ~/Projects/repository/
$ git remote add origin /srv/git/repository.git
$ git push origin master

You only need to run the last command when you issue new commits on your working copy. After that you can poke your friends to pull from it once you start git-daemon.

1.4 Configuring and running git-daemon

Edit the /etc/conf.d/git-daemon.conf file, change the GIT_REPO variable to the /srv/git dir.

Since you're hosting publicly available copies of your repositories, add the --export-all flag to GIT_DAEMON_ARGS.

Now you can run the daemon!

# rc.d start git-daemon

To test it, try to clone using the git:// protocol.

$ cd /tmp
$ git clone git://localhost/repository.git

Try this from another host, obviously changing 'localhost' to your actual hostname, domain name or IP address. If this part doesn't work, check your firewall and/or router configuration.

1.5 Workflow

Now, to ease things up, this would be a draft of your workflow:

  • Do work on your working copy, commit changes
  • Push new commits to the bare repo on /srv/git to make them available as git://yourhostname/repository.git
  • You pull other people's changes into your working copy, then push them to your repo.
  • Repeat until great things are achieved ;)

Note your git-daemon instance only allows pulling. If you work with other people, they should be doing the same and implement some way to let each other know of changes.

You can implement anonymous pushing by adding this to each repository:

$ echo "[daemon]" >> /srv/git/repository.git/config
$ echo "  receivepack = true" >> /srv/git/repository.git/config

This doesn't perform any kind of authentication, so anyone can push changes to this repository. If you want to perform authentication, you can enable SSH on the git user by adding people's public keys into /srv/git/.ssh/authorized_keys or installing something more fine-grained like gitosis or gitolite.

1.6 TODO

  • Allow push over SSH
  • Run cgit/gitweb for web repo browsing
  • Other useful tools: bananajour, bigbananajour, etc.