This will create a new user ‘gitosis’ and prepare a structure for repositories in /srv/gitosis. Now let’s initialize a gitosis-admin repo – it is used for managing repositories and access
Of course there’s always an option to use github. And if you’re working on an open source project, or want to concentrate on coding and not system administration, github is a lot better option than setting up and managing your own git server (I’ve been so impressed by @defunkt‘s presentation on #frozenrails, that started recommending github to everyone 🙂 But if you already pay for a virtual machine somewhere (like Linode), then setting up your own git server might be a viable option, especially that it is sooo easy.
The following instructions have been verified on Ubuntu 10.04 Lucid Lynx, but should work at least on Ubuntu 9.04 and 9.10 just as well.
Let’s start with installing gitosis itself. Issue the following command on the server:
sudo apt-get install gitosis
This will create a new user ‘gitosis’ and prepare a structure for repositories in /srv/gitosis. Now let’s initialize a gitosis-admin repo – it is used for managing repositories and access rights.
sudo -H -u gitosis gitosis-init < ~/tmp/my_public.key
You need to have a public key for accessing it. If you don’t have one yet, you can use
ssh-keygen -t rsa
command on your local machine to generate public/private key pair. Copy public key to the server before initializing gitosis-admin repository.
Now with gitosis-admin repo initialized on the server – let’s clone it to the local computer.
git clone email@example.com:gitosis-admin.git
If you see an error like:
Permission denied (publickey). fatal: The remote end hung up unexpectedly
That is most likely because you restricted access to you server via ssh to only certain users, and gitosis is not one of them. Edit /etc/ssh/sshd_config, find AllowUsers line and add gitosis to the list.
Now that you have successfully cloned gitosis-admin repo to your local computer, you can add new users and new repositories.
To add new repository, edit gitosis.conf and add lines like:
[group myrepo] writable = myrepo members = user@computer
After that commit and push the changes to gitosis-admin project:
git commit -a -m "Added myrepo repository"
Now you can clone this new repository to your local machine (note .git added to the name of the repository):
git clone firstname.lastname@example.org:myrepo.git
To allow new user to access your repository, get this user’s public key, copy it to gitosis-admin/keydir as email@example.com, then edit gitosis.conf and add newuser@computer (without .pub) to the list of members:
[group myrepo] writable = myrepo members = user@computer newuser@computer
Now add new files and commit and push changes to git server (make sure you really add all files and don’t forget to push – these are quite common mistakes when adding new users).
git add . git commit -m "Added user newuser" git push
With a freshly cloned empty repository you’ll need to add you first files, do a commit and push origin master:
vim README.txt git commit -a -m "Added readme" git push origin master
Now you can git pull and git push as much as you like.