February 09, 2016

Setting up Git HTTP Backend for local collaboration

You want to share a topic branch with a colleague but do not want to push that branch upstream to Github/BitBucket/GitLab, etc. How do you do this? You could create a patch and email it. Or you could do it in the most crazy way possible and use Apache and allow your colleague to pull from your repo directly. This does take a bit more time to setup, but it would also be absolutely crazy dumb for everyone involved. Basically, let's setup a git server on your workstation!

First create a place to store your repos. Let's also create a test repo to work with to make sure everything is working.

mkdir -p ~/Sites/git
cd ~/Sites/git
mkdir testproject.git
cd testproject.git
git init --bare

Next let's setup Apache (I am using OS X El Capitan with Apache 2.4).

Edit /private/etc/apache2/httpd.conf.

Ensure the following modules are being loaded.

LoadModule cgi_module libexec/apache2/mod_cgi.so
LoadModule env_module libexec/apache2/mod_env.so

Uncomment the following line:

Include /private/etc/apache2/extra/httpd-vhosts.conf

Edit /private/etc/apache2/extra/httpd-vhosts.conf.

I removed the existing virtualhosts since I actually do all of my development with Vagrant and Linux. So I really have no need to have anything more than a single virtualhost on my Mac.

<VirtualHost *:80>

    DocumentRoot /Users/user/Sites

    <Directory "/Users/user/Sites/">
        Options +Indexes +MultiViews +FollowSymLinks +ExecCGI
        AllowOverride All
        Require all granted

    <Directory "/Library/Developer/CommandLineTools/usr/libexec/git-core/">
        Options +ExecCGI
        Require all granted

    <LocationMatch "^/git/">
        Require all granted

    SetEnv GIT_PROJECT_ROOT /Users/user/Sites/git
    SetEnv REMOTE_USER user
    ScriptAlias /git/ /Library/Developer/CommandLineTools/usr/libexec/git-core/git-http-backend/


Edit the the bolded parts to suit your local setup.

Restart apache.

sudo apachectl restart

Your git repos will now be available at http://locahost/git/<REPONAME.git>.

You should now be able to clone your empty repo.

Let's test it out.

cd ~/Sites
git clone http://localhost/git/testproject.git testproject
cd testproject
You should be able to make changes and push to your remote.
echo '# README' > README.me
git add README.md
git commit -am 'Add Readme'
git push origin master

There it is!

Now you can have a colleague pull changes directly from you. Simply provide them with your public address, for example, and they should now be able to clone your repo, add your remote, pull changes, etc.

If you want other people to be able to push to your repo you will have to explicitly allow this. In your testproject.git repo, set the http.receivepack value to true:

git config http.receivepack true

Did you like this post? Let me know by sending me a message. Is there a topic you would like me to cover? Let me know about that too. I look forward to hearing from you!

Let's Connect!