Local executing hook after a git push?

Viewed 51877

I've looked at the githooks manpage but unless I'm missing something I don't see an option for local, post-push git hooks. I'd like to have one that updates the api docs on my web server (for which I already have a script) after I push the master branch to the GitHub repo. Of course I could just write my own script that combines the git push and the api docs run, but that feels somewhat inelegant.

7 Answers

Implementing a post-push hook is in fact possible by using the reference-transaction hook. After a push is done, git will locally update the remote tracking branch, triggering a reference transaction on refs/remotes/REMOTE/BRANCH.

Here's an example for a hook that uses this technique:

#!/bin/bash
set -eu

while read oldvalue newvalue refname
do
    if [ $1 = committed -a $refname = refs/remotes/origin/main ]
    then
        exec .git/hooks/post-push
    fi
done

This script must have the executable bit and be placed in .git/hooks/reference-transaction. The hook runs after pushing the main branch to origin. Put your actual hook script in .git/hooks/post-push.

Just create a pre-push hook with a sleep at the top. Ensure sleep is long enough for the commits to be uploaded to upstream server based on your network connection speed. Ideally add a & to run your script in the background:

(sleep 30 && .git/hooks/post-push) &
Related