Setup Hosting Git Repositories on Ubuntu Server

Setup Hosting Git Repositories on Ubuntu Server

Git is a distributed revision control and source code management (SCM) system with an emphasis on speed. Git was initially designed and developed by Linus Torvalds for Linux kernel development in 2005. Based on a recent survey of Eclipse IDE users.

Every Git working directory is a full-fledged repository with complete history and full version tracking capabilities, not dependent on network access or a central server.
Git is free software distributed under the terms of the GNU General Public License version 2.

Official Website

Install SSH Server

$ sudo apt-get install openssh-server openssh-client

Install Git

$ sudo apt-get install git-core

Configure Git server, create Git server management user

$ sudo useradd -m git
$ sudo passwd git

Create Git repository storage directory:

$ sudo mkdir /home/git/repositories

Setting Git repository permissions:

$ sudo chown git:git /home/git/repositories
sudo chmod 755 /home/git/repositories

Initialize global settings:

$ git config --global "username"
$ git config --global "username@server"

Install Git Web UI

Install Apache:

$ sudo apt-get install apache2

Install Git Web UI

$ sudo apt-get install gitweb

Configuration Gitweb

The default is no css loaded, the use of static files gitweb connected to DocumentRoot directory:

$ cd /var/www/
$ sudo ln -s /usr/share/gitweb/* .

Modify the configuration

Modify the $projectroot to git repository storage directory (for example: $projectroot = /home/git/repositories),save and refresh your browser.

If you do not find the items that you will need to $projectroot/*. Git attributes to 755, so that apache user has read permissions. You can only change you need to let others through web access that git.

$ sudo chmod 755 -R gitosis-admin.git

Visit: http://localhost/cgi-bin/gitweb.cgi

Modify /etc/gitweb.conf, like following content:

# path to git projects (.git)
# $projectroot = "/var/cache/git";
$projectroot = "/home/git/repositories";

# directory to use for temp files
$git_temp = "/tmp";

# target of the home link on top of all pages
$home_link = $my_uri || "/";

# html text to include at home page
$home_text = "indextext.html";

# file with project list; by default, simply scan the projectroot dir.
$projects_list = $projectroot;

# stylesheet to use
@stylesheets = ("/gitweb/static/gitweb.css");

# javascript code for gitweb
$javascript = "/gitweb/static/gitweb.js";

# logo to use
$logo = "/gitweb/static/git-logo.png";

# the 'favicon'
$favicon = "/gitweb/static/git-favicon.png";

# git-diff-tree(1) options to use for generated patches
#@diff_opts = ("-M");
@diff_opts = ();

Restart Apache

$ sudo service apache2 restart

Visit: http://localhost/cgi-bin/gitweb.cgi

Setup Hosting Git Repositories on Ubuntu Server

Install Gitosis

Install the Python's setup tools:

$ sudo apt-get install python-setuptools

Install Gitosis:

$ cd /tmp
$ git clone
$ cd gitosis
$ sudo python install

Add user for Git

$ sudo adduser \
    --system \
    --shell /bin/sh \
    --gecos 'git version control' \
    --group \
    --disabled-password \
    --home /home/git \

Generate native key, switch to the localhost, if there ~/.ssh/ skip this step:

$ ssh-keygen -t rsa

Upload the key to a temporary directory server:

$ scp ~/.ssh/ username@server:/tmp

Initialize gitosis (On Git server):

$ sudo -H -u git gitosis-init < /tmp/

Modify the post-update privileges:

$ sudo chmod 755 /home/git/repositories/gitosis-admin.git/hooks/post-update

Clone gitosis management platform on localhost:

$ git clone username@server:gitosis-admin.git
$ cd gitosis-admin

By modifying the gitosis-admin user rights management gitosis, add a public secret to keydir, add users. After modification commit, push to the server to complete the repositories permissions related operations.


Goal: Add User belief and repositories teamwork to gitosis, and cooperative management administrator xuri.

  • User belief add and send it to xuri
$ belief:~$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/belief/.ssh/id_rsa):
Created directory '/home/belief/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/belief/.ssh/id_rsa.
Your public key has been saved in /home/belief/.ssh/
$ belief:~$ cp /home/belief/.ssh/ /tmp
  • Gitosis administrator belief assign privileges to xuri
xuri:~$ cd ~/projects
$ git clone [email protected]:gitosis-admin
$ cd gitosis-admin
$ cat gitosis.conf

[group gitosis-admin]
writable = gitosis-admin
members = xuri@ubuntu

ls keydir/
[email protected]
cp /tmp/ keydir/
nano gitosis.conf

[group gitosis-admin]
writable = gitosis-admin
members = xuri@ubuntu

[group teamwork]
writable = teamwork
members = xuri@ubuntu belief

$ git add .
$ git commit -am "add member belief and project teamwork"
$ git push
  • User xuri add Project teamwork
xuri:~$ cd ~/projects
$ mkdir teamwork
$ cd teamwork
$ git init
$ touch readme.txt
$ git add readme.txt
$ git commit -am 'first commit'
$ git remote add origin [email protected]:teamwork.git
$ git push origin master
  • User belief clone teamwork and modify readme.txt
belief:~$ git clone [email protected]:teamwork.git
$ cd teamwork
$ ls
$ date > readme.txt
$ git commit -am 'add time to readme.txt' && git push
  • Users xuri pull teamwork
xuri:~/projects/teamwork$ nano .git/config
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = [email protected]:teamwork.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master

$ git pull
5.00 avg. rating (98% score) - 1 vote