Solved: Running git pull from a php script causing an error?

I wanted to improve my Git workflow, so I went about using GitHub’s Service Hooks to set up a web hook to my development server. The idea being that I would have GitHub ping my server every time I or anyone else pushes to the master GitHub repository.

The idea was pretty simple. I would have a php file located on my development server with following command.

<?php shell_exec('git pull'); ?>

However, I quickly ran into the following permission error.

error: cannot open .git/FETCH_HEAD: Permission denied

The reason being was that my server’s Apache user doesn’t and shouldn’t have access to the root. However, to run git pull, it was trying to access the host keys, which are stored in the ~/.ssh/known_hosts file. As you can see, these files are located in the root.

So I was scratching my head for a solution when I came across this terrific tutorial, Github playground servers and auto pull from master, posted by Konstantin Kovshenin. Konstantin proposed a simple, but brilliant, solution of using the GitHub service hook to ping a local php file that would signal a server cron job to handle the git pull. If you are trying to do the same thing, and experienced the above error, follow the Konstantin’s tutorial and it should work for you.

Final notes: having a development server automatically pull from the Git repository is very useful, however it is not a good idea for production servers. It is more secure and more controlled to manually update your production servers git repository from the command line.

Great video lessons on the basics of using Git version control

Git is a fast version control system. I am sure you are probably already familiar with it, but if you are not you can read about it on the official website here. Basically Git is a really useful way of backing up projects and saving ‘snapshots’ in the case where you might need to go back and reuse an old file because the new file was badly written.

The problem with Git is that it has a big learning curve. However, I found some great video tutorials created by Jason Semko on Youtube. The videos are really easy to follow and offer step-by-step instructions on the basics of using Git. If you would like to use a great version control system or if you are knew to git, here are the videos to watch.

How to use Git – Git Video Tutorial

Part 0 (Installation)

How to use Git – Git Video Tutorial

Part 1 (add, commit, push, pull, status)

How to use Git – Git Video Tutorial

Part 2 (log, reflog, show, gitk, checkout, reset)

How to use Git – Git Video Tutorial

Part 3 (The basic file flow, rm, add, commit, gitignore)

How to use Git – Git Video Tutorial

Part 3.5 (Setting up a new user account)

How to use Git – Git Video Tutorial

Part 4 (Merge Conflicts)

How to use Git – Git Video Tutorial

Part 5 (More Merge Conflicts)

How to use Git – Git Video Tutorial

Part 6 (branch, checkout, remote)

Huge thanks to Jason Semko for making these videos. Visit his channel here.