Git over http(s)

Traditionally git used to work only over ssh or git protocols while there was only a dumb version of git over http which was slow and inefficient. While this was ok for most of the time sometimes git needs to be able to work over http. Now starting from git 1.7 both git servers and clients have support for smart http which works over http(s) and is supposed to be as efficient as the ssh version.

This functionality is made available by a cgi script called git-http-backend provided with git-core. So for git to work over http(s) there should be a web server already configured and as a result there won’t be any conflicts by both the web server and git trying acquire port 80.

The manual for the git-http-backend can be found here.

The following steps can be used to configure git to work over http(s) with Apache.

1) First configure Apache

Make sure mod_cgi, mod_alias, and mod_env are enabled.

Open the Apache config file and append the following. Debian based system should have it under /etc/apache2/apache2.conf by default

SetEnv GIT_PROJECT_ROOT /home/user/git_pub
ScriptAlias /git/ /usr/lib/git-core/git-http-backend/

The GIT_PROJECT_ROOT should point to the root folder where git repositories would be hosted. Set this away from the document root of the web server. What the above do is direct any requests with /git/ to the git-http-backend and tell the script that the root of git repositories is GIT_PROJECT_ROOT.

That is all that needs to be done that is specific to git over http(s). The manual for for the git-http-backend explains these steps pretty thoroughly.

Now for some tit-bits that are not explained in the manual. Those who are experienced with Apache and Git would find the following very boring.

2) For authentication for both read and write accesses append the following to theApache config file

<Location /git>
AuthType Basic
AuthName “Private Git Access”
AuthUserFile /etc/apache2/authusers
Require valid-user

What the above do is make requests to /git only accessible to valid users and tell valid users are listed on the file /etc/apache2/authusers. Make sure the file authusers is accessible by Apache.

If there is no AuthUserFile in your system the following command can be used to create the user list at /etc/apache2/authusers and add the user ‘username’ to it. The command will prompt for a password for the user.

htpasswd -c /etc/apache2/authusers username

3) Restart Apache

On debian most probably, sudo /etc/init.d/apache2 restart

4) Create an empy bare git repository under the specified GIT_PROJECT_ROOT (/home/user/git_pub in our example)


mkdir project

cd project

git init –bare

5) Make the folder ‘project’ and it’s children owned by the user which the web server is run from. This should be done for push requests by clients to work or otherwise the web server won’t be able to merge files. On debian based systems this user is usually www-data and is defined in a file called envvars under apache2 installation.

sudo chown -R www-data project/
sudo chgrp -R www-data project/

Now the bare git repository should be pull-able and pushable by authorized users.

6) Clone the git repository over http(s) from another location

git clone http://username@host/git/project

7) Do the first commit

cd project
touch readme
git add readme
git commit -m “first commit”
git push origin master

It is as easy as that. From here the setuped git repository should work as normal.


17 responses to “Git over http(s)

  1. Thank you..really informative!!

  2. thanks. it works! easy and fast.

  3. Thanks, it works very well. However, I’m wondering whether can I know who commits what from any source of information? Just now I can only see user-defined names by using “git log” but they can be easily changed, even to other people’s name, which is less authenticated. Do you have any idea?

  4. could you please update the steps for windows…

  5. @Xiao Jia
    No way I know of.

  6. “TID BITS” not “TIT BITS”. Oops!

    Please fix your article and delete this comment. šŸ™‚

  7. @Anon of October 29, 2011

    Made tit bits to tit-bits. lol.

    Aren’t tid-bit and tit-bit the same thing?

  8. @Kashun

    maybe, a tit-bit šŸ™‚

  9. Rahul Chandrashekar

    Can this be used for communication trough OSLC also.
    Please let know your feedback.


  10. @Rahul

    Sorry I’m not sure what OSLC is.

  11. Thank you very much, according to your method, it works very well!

  12. Thank you! I have been having headaches over this all week!

  13. Thanks a lot! You saved me a lot of time!

  14. Michael Lewis

    Thank you very much for writing this, after hours of messing around with different suggestions, I found this page – and it worked straight away.

  15. Pingback: Make Git work on HTTP protocol | Eureka!

  16. I struggled with this official documentation for days and then you explained that so well. I get that in just 10 minutes! THANK YOU

  17. Thank you so much for this!!! I actually read about 20 different sources and all of them provided confusing information which was sometimes nonsense. You provided the required information with the best detail and as simple as possible. I don’t know how to express my gratitude. Thanks again!!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s