How to add personal git repository in a web hosting server

The most useful site for the topic is http://www.mobiphil.com/2010/03/git-on-shared-hosting-with-git-http-backend/.

If your web hosting server does not have git, you can just manually install the git. (I don’t want to describe this at this posting.)

1. Prerequisite

Check following CLI commands are available in the server

/usr/bin/git
/usr/libexec/git-core/git-http-backend

2. The goal

Here is the one that I want to achieve.

  • ${HOME}/git_projects in the server
    • This is the actual git repositories such as xxx.git, yyy.git, etc.
  • ${HOME}/public_html/git in the server
    • This is the http address that a user can access
  • A user can use the http address with proper userID and password

3. ${HOME}/git_projects in the server

Do this:


cd ${HOME}/git_projects
mkdir xxx.git
cd xxx.git
git init --bare --shared=all
touch git-daemon-export-ok

Now, the xxx.git is ready to use.

4. ${HOME}/public_html/git in the server

.htaccess


Options +ExecCgi
AuthName "Private Git Access"
AuthType Basic
AuthUserFile ${HOME}/public_html/git/.htpasswd
Require valid-user
RewriteEngine on
RewriteBase /
SetHandler cgi-script
RewriteRule ^([a-zA-Z0-9._]*\.git/(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))$ ./git-http-backend.sh/$1

git-http-backend.sh


#!/bin/sh
#first we export the GIT_PROJECT_ROOT
export GIT_PROJECT_ROOT=${HOME}/git_projects
#and run your git-http-backend
/usr/libexec/git-core/git-http-backend

#uncomment the following line if you want to see environment variable values
#echo $REMOTE_USER $QUERY_STRING $REQUEST_METHOD $PATH_INFO >> $HOME/logs/log

The git-http-backend.sh should have +x permission.

.htpasswd

You need to create it by yourself. http://www.htaccesstools.com/htpasswd-generator/ is a good site to create the .htpasswd.

5. Client

Create a directory and get the repository. And, git push at the end. Here is showing an example.


mkdir ~/tmp
cd ~/tmp
git clone http://[ID]@git.example.com/xxx.git
cd xxx
...(modify or add files)
git add .
git commit -m "Initial Commit"
git push origin master

Note that “git clone http://[ID]@git.example.com:cgi-bin/xxx.git” will ask you password.

Also, after this, other user can clone the repository by git clone http://[ID]@git.example.com/xxx.git in anywhere, and “git push” and “git pull” can be used.

6. Other notes

From time to time, you may encounter 403 error. In this case, you need to check .htaccess in the root web site directory (normally, ~/public_html/.htaccess) to check if it has like this:


<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>

You May Also Like

2 Comments

  1. This is the post just what I was looking around and finally I’ve get here. I will make a discussion with my boss about this issue for understanding perfectly but thanks a lot for sharing this precious allocation very wisely. Keep it up.

Leave a Reply

Your email address will not be published. Required fields are marked *