How do I hook a git pull on the remote?

Danny picture Danny · May 8, 2010 · Viewed 8k times · Source

Is there a way to hook when a git pull happens on the remote (similar to a pre-receive or post-receive). Basically I'd like to be able to cause the remote to commit whatever it has when there is a pull.

In my situation, whatever is live on the remote is an authoritative source which may get modified without a git commit. I want to make sure when I pull I'm always able to get the latest of whatever is live.

Answer

mrj picture mrj · Jan 5, 2012

First, to answer your actual question: there are no hooks invoked on the remote side when someone fetches. (When someone pulls, all the remote knows is that they fetched from it - it doesn't know whether they ran git pull, git fetch, git remote update...)

As for your actual situation: I agree with Magnus that it'd be best to simply have commits take place after edits, or failing that, have some sort of periodic task (cronjob?) that checks for modifications and commits if it finds any. If you don't like either of those choices, you're left with streamlining things so that it's quick and easy for you to trigger a commit in the remote repository just before pulling.

Also, I'd suggest not regarding a repository with a work tree as your canonical repository. It's asking for trouble if you ever need to push to it. Instead, you could have a bare canonical repository which your live repository pushes to after committing, and install a post-receive hook in that bare repository to update the live repository when necessary.