Setting up your own GIT server

image

If you are planning to have your own GIT setup, this blog details out the steps involved. You can install on a windows or a linux box. But we recommend that you use linux as Gitolite(detailed below) runs only on linux.

Installing on Ubuntu Server:-

Step 1 (Open Terminal and Run below command to Install GIT Package):-
sudo apt-get install git

Step 2 (Run below command to Add GIT admin user):-
sudo adduser \
--system \
--shell /bin/bash \
--gecos 'git version control' \
--group \
--disabled-password \
--home /home/git git

For Access Control, We use Gitolite (It allows you to setup git hosting on a central server, with fine-grained access control and many more powerful features)

Server Requirements for Gitolite :-

any unix system
sh
git 1.6.6 or later
perl 5.8.8 or later
openssh 5.0 or later
a dedicated userid to host the repos (in this document, we assume it is "git", but it can be anything; substitute accordingly)
this user id does NOT currently have any ssh pubkey-based access
ideally, this user id has shell access ONLY by "su - git" from some other userid on the same server (this ensure minimal confusion for ssh newbies!)

Install & Setup gitolite into git server:-

Step 3 :-
sudo su - git #Switch to git user
[sudo] password for syscon:

mkdir ~/bin #make bin folder

git clone git://github.com/sitaramc/gitolite #Download gitolite

gitolite/install -ln ~/bin #Create Symbolic link

For Admin Access need to generate the authorization key

Run below command on the local machine (Ubuntu):-

$ ssh-keygen -t rsa -f ~/.ssh/gitadmin -C "gitadmin"
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in gitadmin.
Your public key has been saved in gitadmin.pub.
The key fingerprint is:
SHA256:8PenW/8q9+DeYJj36whYbiQFwX1JuYE086xlMH0qph0 gitadmin
The key's randomart image is:
+--[ RSA 2048]----+
|+oE.oo...        |
|o+o=oo.o .       |
| +o.+ + o        |
|  .o . o         |
|      o S        |
|     . +         |
|      o          |
|                 |
|                 |
+-----------------+

We can see that the 2 keys have been created.

$ ls -l ~/.ssh/gitadmin*
-rw-r--r-- 1 gittest gittest  390 Jan 10 09:39 /home/gittest/.ssh/gitadmin.pub
-rw------- 1 gittest gittest 1.7K Jan 10 09:39 /home/gittest/.ssh/gitadmin

“gitadmin” is the private key and “gitolite.pub” is the public key.

Upload the public key to the root user’s account on the remote machine.

~/.ssh$
$ scp ~/.ssh/gitadmin.pub git@:
git@gitserver's password:
gitadmin.pub

Run below command on the Git Server:-

export PATH=/home/git/bin:$PATH #Add gitolite access permanently

So, let’s run that command-line tool to set up our gitolite server, giving it the public key that we uploaded, called “gitadmin.pub”.

git@localhost:~$ /home/git/bin/gitolite setup -pkgitadmin.pub
Initialized empty Git repository in /home/git/repositories/gitolite-admin.git/    
Initialized empty Git repository in /home/git/repositories/testing.git/
WARNING: /home/git/.ssh missing; creating a new one                        
WARNING: /home/git/.ssh/authorized_keys missing; creating a new one

In to Local System:-*

git clone git@gitserver:gitolite-admin

#For Alias need to add below entry into your config

vi ~/.ssh/config

Host gitbox
  Hostname gitserver
  User git
  IdentityFile ~/.ssh/gitadmin

Config File and Access Control Rules

Once the install is done, you switch to the gitolite-admin clone you just made on your workstation, and poke around to see what you got:

$ cd ~/gitolite-admin/
$ ls
conf/  keydir/
$ find conf keydir -type f
conf/gitolite.conf
keydir/gitadmin.pub
$ cat conf/gitolite.conf

repo gitolite-admin
    RW+                 = gitadmin

repo testing
    RW+                 = @all

To clone git repository into your local system:-

git clone git@:testing
Cloning into 'testing'...
remote: Counting objects: 261, done.
remote: Compressing objects: 100% (195/195), done.
remote: Total 261 (delta 91), reused 0 (delta 0)
Receiving objects: 100% (261/261), 24.16 KiB | 0 bytes/s, done.
Resolving deltas: 100% (91/91), done.
Checking connectivity... done

#testing folder created into your system

Repositories and User Management
$ ssh-keygen -t rsa -f ~/.ssh/gituser1 -C "gituser1"
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in gitadmin.
Your public key has been saved in gituser1.pub.
The key fingerprint is:
SHA256:8PenW/8q9+DeYJj36whYbiQFwX1JuYE086xlMH0qph0 gituser1
The key's randomart image is:
+--[ RSA 2048]----+
|+oE.oo...        |
|o+o=oo.o .       |
| +o.+ + o        |
|  .o . o         |
|      o S        |
|     . +         |
|      o          |
|                 |
|                 |
+-----------------+

Creating a new repository into gitolite & Giving Access to user :-

scp gituser1.pub git@gitserver: #Copy gituser1 public Auth key file into git server
ssh gitadmin@gitserver #Login to gitadmin Server
cd gitolite-admin 
cp /home/git/gituser1.pub keydir/gituser1.pub #Copy gituser1 public Auth key into gitolite admin repo
# Creating a new Repository
vi conf/gitolite.conf # Open gitolite configuration file
repo gitolite-admin
    RW+                 = gitadmin
repo newrepo
    RW+                 = gituser1
repo testing
    RW+                 = @all
#Save above file 
git add conf/ keydir/
git commit -m "Create new repo and give access to gituser1"
git push
Counting objects: 7, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 392 bytes | 0 bytes/s, done.
Total 4 (delta 1), reused 0 (delta 0)
remote: Initialized empty Git repository in /home/git/repositories/newrepo.git/
To git@gitserver:gitolite-admin
   383e46f..ba3c4aa  master -> master

#newrepo is created and gituser1 have access to that repository

ssh git@gitserver #To Check repository access

PTY allocation request failed on channel 0
hello gitserver, this is git@syscon-ThinkCentre-E73 running gitolite3 v3.6.8-3-g29d5bb7 on git 1.9.1

R W newrepo
R W testing
Connection to gitserver closed.

Now, gituser1 able to checkout newrepo and its ready for start working a git repository

git clone git@gitserver:/newrepo

Cloning into 'newrepo'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.

Hope this gives you a headstart for having your own git setup.