git over http on shared hosting

How To Git over http

So I have read of about a hundred of tutorials and went over the git manual about the same amount of times but for some reason i just couldn't get it to work on an Hostgator shared hosting account So to save you or anyone who comes to this problem some time here is How to git over http (git-http-backend) on a shared hosting account. Since i figured it out my version control life is just awesome .

Now i used HostGator for many of my clients and recommend them (we a few others) for most small to mid size WordPress sites since their shared hosting account packages are more then enough for these kind of sites and the price range is also fair.

This is for working with an hostgator shared hosting account but will work on most major hosting companies shared hosting accounts,assuming that:

  • git is installed on the server.
  • Repositories are located under /public_html/repos/
  • you have Created .htpasswd file with a user
  • Repositories are created or cloned as bare repositories (-- bare)

create a new directory git and inside create a new file named git.cgi with

#!/bin/sh
if [[ $REQUEST_URI != /http/* ]]
then
echo Status: 403 Forbidden
echo Content-type: text/plain
echo
echo Direct access to git.cgi is not allowed
exit
fi

export GIT_HTTP_EXPORT_ALL
export GIT_PROJECT_ROOT=/home/<<USERNAME>>/public_html/repos/

if [ -z "$REMOTE_USER" ]
then
export REMOTE_USER=$REDIRECT_REMOTE_USER
fi

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

Change <<USERNAME>> to your hostgator user name.
Then create a new file named .htaccess with

Options +ExecCgi
RewriteEngine on
AuthType Basic
AuthName "My Private Git Repos"
AuthUserFile "/home/<<USERNAME>>/pathto/.htpasswd"
require valid-user
RewriteBase /git
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/git.cgi/$1

Once again change <<USERNAME>> to your hostgator user name and pathto/.htpasswd to the correct path.

Next is probably the most annoying part but its important, inside your git repository inside the .git directory create a new empty file named git-daemon-export-ok

and if your git directory is located at the root of your domain (something like pubic_html/git) then you can now Access that repository over http ex:

git clone YOUR_DOMAIN.COM/git/repositoryname
#or if that doesn't work try 
git clone YOUR_DOMAIN.COM/git/git.cgi/repositoryname

then you can push, pull whatever and the best part is that it Works on most shared hosting accounts (well the bigger companies like BLUEHOST, HOSTMONSTER and DREAMHOST).

Whats Next?
With a little modification you can turn this tutorial and gitlist into your own "Github" clone hosted on shared hosting, which i promise to write a full detailed tutorial for that when i get some free time on my hands but for now here are some screenshots:

Self hosted github clone oh shared hosting Self hosted github clone oh shared hosting Self hosted github clone oh shared hosting
Self hosted github clone oh shared hosting Self hosted github clone oh shared hosting

Incoming search terms:

Ohad Raz

WordPress Consultant, a WordPress Developer and a WordPress Freelancer With over 10 years experience in architecting web sites and applications. WordPress Development moderator and somethimes Plugin Developer.