Setting up a Git repository on Debian

Posted on 23-06-2015 12:51 by graham
This tutorial explains the complete process of installing and setting up a Git server that will host your Git repository.

Install Git

Install Git SCM using the following command:
apt-get install git-core

Now configure Git:
git config --global "SomeUser"
git config --global

Create a Git repository

Now pick a location where you want to keep the actual repository. I tend to have multiple repositories organized in one folder, so I call the folder git-repos, and put it under /var/www. You can do all this using the following commands:
cd /var/www
mkdir git-repos
cd git-repos
mkdir my-repo.git

Now you have a repo directory created at /var/www/git-repos/my-repo.git. The next thing is to initialize an empty repository there:
cd /var/www/git-repos/my-repo.git
git init --bare

Some more tweaks are necessary:
git update-server-info
cp ./hooks/post-update.sample ./hooks/post-update

Set permissions

The Git directory will be accessed and modified by the www-data user that runs the Apache server process. First, make the repository accessible to all group members:
git config code.sharedRepository group

and then change the access rights to the directory:
cd /var/www/git-repos/my-repo.git
chgrp -R groupname .
chmod -R g+rwX .
find . -type d -exec chmod g+s '{}' +

Note: if you forget the set the permissions in the way described above, you will get the following message while trying to push changes to the repo:
Counting objects: 3, done.
Writing objects: 100% (3/3), 217 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: error: insufficient permission for adding an object to repository database objects
remote: fatal: failed to write object
error: unpack failed: unpack-objects abnormal exit
! [remote rejected] master -> master (unpacker error)
error: failed to push some refs to ''

Configure Apache access to Git

You will want to configure Apache to be able to access the Git repo. Start by creating a password file in Apache:
htpasswd -c /etc/apache2/passwd.git someuser

Then, modify your Apache configuration by adding the following section:
<VirtualHost *:80>

DocumentRoot "/var/www/git-repos"

SetEnv GIT_PROJECT_ROOT /var/www/git-repos
#ScriptAlias /git/ /usr/lib/git-core/git-http-backend/
ScriptAliasMatch \
"(?x)^/(.*/(HEAD | \
info/refs | \
objects/(info/[^/]+ | \
[0-9a-f]{2}/[0-9a-f]{38} | \
pack/pack-[0-9a-f]{40}\.(pack|idx)) | \
git-(upload|receive)-pack))$" \

<Directory "/usr/lib/git-core*">
Options ExecCGI Indexes
Order allow,deny
Allow from all
Require all granted

<LocationMatch "^/git/.*/git-receive-pack$">
AuthType Basic
AuthName "Git Access"
AuthUserFile "/etc/apache2/passwd.git"
Require valid-user

<Directory "/var/www/git-repos">
Options +ExecCGI +FollowSymLinks +SymLinksIfOwnerMatch
AllowOverride All
order allow,deny
Allow from all
AddHandler cgi-script cgi
DirectoryIndex gitweb.cgi

AuthType Basic
AuthName "git"
AuthUserFile "/etc/apache2/passwd.git"
Require valid-user


If you want to restrict read access to your repository to only authenticated users, add the following section in the setup above:
<VirtualHost *:80>


<Location /my-repo.git>
AuthType Basic
AuthName "Git Access"
AuthUserFile "/etc/apache2/passwd.git"
Require valid-user


Test your setup

You can now clone your repo, make some changes and commit them:
git clone
git add .
git commit -m "Some comment"
git push origin master


Add comment

Has this tutorial been helpful to you? Or do you see anything wrong? We appreciate your opinion!
Your comment:
Show formatting hints
HTML is disallowed, but in your text you can use the following markup
  • [code][/code] for a block of code
  • [tt][/tt] for inline code
  • [link]link href|link anchor[/link] for links
  • [b][/b] for bold text
+ Ask a question
If you have a technical question related to programming and computers, ask it here. Other users will help you solve it!
Unanswered questions
Share your knowledge by helping others solve their problems