Skip to content

Private Git repositories with Gitolite

Set up a self-hosted Git server with Gitolite.

4 min read

Suppose you want to manage private Git repositories for a project, team, or company.

Hosted services such as GitHub , Bitbucket , and GitLab  are convenient. But if, like me, you're a bit of a control freak, you may prefer a simpler way to administer private repositories while keeping your source code under your control.

Gitolite  is small, simple, and powerful. User access control and repository creation are only a git commit and git push away.

This guide shows you how to set up private Git infrastructure with Gitolite and easily manage repositories and users.

Inventory

What do you need for this tutorial?

  • A publicly accessible server. If you don't have one, you can get a VPS (virtual private server) from Linode , DigitalOcean , or a similar provider.
  • An account with sudo access.

Assume your local ~/.ssh/config file contains:

~/.ssh/config
Host git-server
# This is the IP for your VPS
HostName 111.111.111.111
Port 22
# This is the remote user
User yolo

Installation

First, log in to the remote machine where you want to set up Gitolite:

Shell command
ssh git-server

Install Git:

Shell command
sudo apt install git

Create a dedicated user to manage the repositories and enforce the access-control rules, then disable password access for that user:

Shell command
sudo adduser git
sudo passwd -l git

On your local machine, open a new terminal and copy your public key to the remote machine:

Shell command
scp ~/.ssh/id_rsa.pub git-server:

If the copy succeeds, id_rsa.pub will be in the remote yolo user's home directory.

The adduser command created /home/git. Now create its .ssh directory, move the public key to /home/git/.ssh/yolo.pub, and set the directory's permissions and ownership:

Shell command
sudo mkdir -p /home/git/.ssh
sudo mv ~/id_rsa.pub /home/git/.ssh/yolo.pub
sudo chmod 700 /home/git/.ssh
sudo chown -R git:git /home/git/

Switch to the git user, then clone and install Gitolite:

Shell command
sudo su git -l
git clone https://github.com/sitaramc/gitolite ~/gitolite
mkdir ~/bin
~/gitolite/install -to ~/bin

Log out of and back into the git user's shell so the commands installed in ~/bin become available. Then set up Gitolite with yolo.pub as the administrator key:

Shell command
exit
sudo su git -l
gitolite setup -pk ~/.ssh/yolo.pub
exit
exit

Leave the git user's shell and the remote machine. You need to run exit twice because you are two levels deep (yolo -> git).

Debian note

If the scripts in ~/bin aren't picked up automatically, you might need to create a ~/.bash_profile file and add ~/bin to $PATH:

Bash
export PATH=$PATH:~/bin

Ubuntu picks up the scripts in ~/bin automatically.

Now clone the gitolite-admin administration repository:

Shell command
git clone git@git-server:gitolite-admin ~/gitolite-admin

You can now create repositories and grant users access with their public SSH keys.

Repositories and user management

Get the public keys from users who need Git access and place them in ~/gitolite-admin/keydir. Name each key after its user, such as tom.pub and jerry.pub, because you will use these names when configuring access.

New repository and user access

To create a repository named yolo-project, give yourself full access, and grant tom and jerry read-write access, add these lines to ~/gitolite-admin/conf/gitolite.conf:

~/gitolite-admin/conf/gitolite.conf
repo yolo-project
RW+ = yolo
RW = tom jerry

Then commit and push the changes to the remote gitolite-admin repository:

Shell command
cd ~/gitolite-admin
git add .
git commit -m "Add new repo, add new keys, give access"
git push

Gitolite will create the repository and control access according to your rules.

Cloning the new repo

It's as easy as:

Shell command
git clone git@git-server:yolo-project

Now you can add content to it and use git as usual.

Upload an existing repository

To upload an existing Git project instead, change to its directory and add the remote:

Shell command
git remote add origin git@git-server:yolo-project

Verify the remote setup:

Shell command
git remote -v

Finally, push your commits and set up tracking for the main branch:

Shell command
git push --set-upstream origin main

You're done. You now have self-hosted infrastructure for private Git repositories and can manage repositories and users with git commit and git push.

Check repository access

To see which repositories you can access, run:

Shell command
ssh git@git-server info

More posts connected by shared tags.