How to setup and use Gitolite with Git

I have been fiddling around with git lately. Git is a Source Code Management and Version Control System, VCS in short, which is fast and very efficient and what sets it apart from other VCS’s is the fact that it stores snapshots of information unlike other VCS’s, which keep difference between the latest updated information and old information and you don’t need to have a connection to the git repository all the time to commit any changes, you can commit the changes locally and at a later time when you are connected to the repository, you can push the changes that you committed earlier. If you want to learn more about Git, I would suggest that you check out Git’s official website and read this book. You can also check out this very cool tutorial by the same guys. Gitolite simply eases the hosting and management tasks for repositories.

For me, the confusing part was where (server or client machine) to run which command, so I’m gonna lay down everything as clearly as I can.  To install git and gitolite on your server, type:
sudo apt-get install git gitolite

This will install git and gitolite on your system but gitolite wouldn’t be set up at this time so you’ll get something like this in the output
Setting up git (1:1.7.9.5-1) ...
Setting up gitolite (2.2-1) ...
No adminkey given - not setting up gitolite.

No admin key given with gitolite

No admin key given with gitolite

Next, you have to generate ssh-key pair for the user on client machine, if you don’t already have one. This client machine can be a different system or the same system as the server. To generate ssh-keypair, type:
ssh-keygen -t rsa

Or you can use -N switch with ssh-keygen to provide passphrase and -f to provide its location. To generate keypair without any passphrase and its name as id_rsa and id_rsa.pub in ~/.ssh directory, type:
ssh-keygen -N "" -f ~/.ssh/id_rsa

ssh keypair generation

ssh keypair generation

Since, both my client and server are the same machine, I’ll just copy the public key, id_rsa.pub, to /tmp.
cp ~/.ssh/id_rsa.pub /tmp

If your server and clients happen to be different, then create a file with .pub extension in /tmp directory and paste the contents of the user’s public key into this file. We’ll be using this file later.

Now, add a user to access gitolite with
sudo adduser \
--system \
--shell /bin/bash \
--gecos 'gitolite user' \
--group \
--disabled-password \
--home /home/git \
git

git user creation

git user creation

Now, login as this user
sudo su git

Set the PATH variable for this user
echo "PATH=$HOME/bin:$PATH" > ~/.bashrc

Source your .bashrc or logout/login for the changes to take effect. To source .bashrc, first go to the home directory and then type:
cd; source .bashrc

Now, all we need to do is setup gitolite. Remember the public key that we copied to /tmp, now, we need to use that
gl-setup /tmp/id_rsa.pub

It will prompt you to edit gitolite.rc file, but default settings are just fine.

Once gitolite is setup, it will create a file projects.list and a directory named repositories with two more directories gitolite-admin.git and testing.git.

gitolite setup with key

gitolite setup with key

Gitolite will be managed by gitolite-admin directory but not directly; user, whose public key you provided in the gitolite setup, will have to clone gitolite-admin directory and changes are to be made to this cloned directory and then pushed. For cloning the directory, type:
git clone git@<hostname>:gitolite-admin

OR

git clone git@<ip>:gitolite-admin

cloning gitolite-admin directory

cloning gitolite-admin directory

Now, everything will be done in the cloned gitolite-admin directory.

Adding Users

To add a new user, get the user’s public key. Create a file with .pub extension in gitolite-admin/keydir/ and paste the contents of the new user’s public key to this file. Then do a git add, commit and then push the changes. You can read more about it here.

adding user to gitolite - steps

adding user to gitolite – steps

windowuser pub contains contents of public key of window user

windowuser pub contains contents of public key of window user

Adding Repositories

To add a repository, you have to edit gitolite.conf file located at gitolite-admin/conf/ directory. Add the project along with atleast one user. The repository doesn’t have to be present, gitolite will automatically create and initialize an empty git repository. Make an entry like this
repo newproject
RW+ = id_rsa
R = windowuser

id_rsa and windowuser are name of the files in gitolite-admin/keydir directory without the pub.

After making the changes, again do a git add, commit and push. You can read more about it here.

adding repositories - steps

adding repositories – steps

adding a repository in gitolite conf file

adding a repository in gitolite conf file

If you have added a user and  it asks for a password then there must be something wrong with your configuration.

If you want to see your access rights to different projects, simply type:

ssh git@<hostname> info

access rights to git repositories

access rights to git repositories

This is it for now.

~Later~

Sugandh

Advertisements

4 thoughts on “How to setup and use Gitolite with Git

  1. At the time of pushing, I get an error

    Counting objects: 7, done.
    Delta compression using up to 8 threads.
    Compressing objects: 100% (3/3), done.
    Writing objects: 100% (4/4), 389 bytes, done.
    Total 4 (delta 0), reused 0 (delta 0)
    remote: Empty compile time value given to use lib at hooks/update line 6
    remote: Use of uninitialized value in require at hooks/update line 7.
    remote: Can’t locate Gitolite/Hooks/Update.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl .) at hooks/update line 7.
    remote: BEGIN failed–compilation aborted at hooks/update line 7.
    remote: error: hook declined to update refs/heads/master
    To git@githost:repositories/gitolite-admin.git
    ! [remote rejected] master -> master (hook declined)
    error: failed to push some refs to ‘git@githost:repositories/gitolite-admin.git’

    Can you help with this?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s