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 user.name "SomeUser"
git config --global user.email someuser@raimme.com

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
To http://git.raimme.com/my-repo.git
! [remote rejected] master -> master (unpacker error)
error: failed to push some refs to 'http://git.raimme.com/my-repo.git'

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>

ServerAdmin webmaster@mysite.com
DocumentRoot "/var/www/git-repos"
ServerName git.raimme.com

SetEnv GIT_PROJECT_ROOT /var/www/git-repos
SetEnv GIT_HTTP_EXPORT_ALL
SetEnv REMOTE_USER=$REDIRECT_REMOTE_USER
#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))$" \
"/usr/lib/git-core/git-http-backend/$1"

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

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

<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
</Directory>

</VirtualHost>

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
</Location>

</VirtualHost>

Test your setup


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

 

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
Email:
+ 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