Manually trigger a git post-receive hook for testing

Using a post-receive hook is a good way to perform a scripted action each time new changes are pushed to a git repo. (If you have not yet set up a basic post-receive hook, consider consulting this article.)

If you’re interested in adding more to your post-receive hook, you might want to have a quick way to test it, without adding & pushing a bunch of test commits. We can trigger it manually with this advice from Kris Jordan.

When you’re working on a post-receive hook, it’s annoying to muck up your project’s commit history and push each time you make a change. Luckily, because it’s just a script, we can fake it from the command-line.

cd ../remote
git log -2 --format=oneline --reverse

First, we need to get the IDs of our most recent 2 commits. The git log command, above, will give us these two IDs in the order you’ll want to replace the $FROM_ID and $TO_ID variables with, respectively.

echo "$FROM_ID $TO_ID refs/heads/master" | ./hooks/post-receive

This method makes setting up your post-receive hooks enjoyable, enabling you to quickly iterate on your script and execute it repeatedly.

1 thought on “Manually trigger a git post-receive hook for testing”

  1. Thanks for you post. I needed something similar.
    In case that is useful to others, here is a small script I made to automate the process:
    ───────────────────────────────────────────────────────────────────────────────────
    #!/bin/bash
    cd /remote_repo/
    oldrev=$(git show HEAD~1 –pretty=’%H’ –no-patch)
    newrev=$(git show HEAD –pretty=’%H’ –no-patch)
    refname=$(git log -1 –oneline –all –pretty=’%S’)
    # debug
    echo -e “oldrev newrev refname \n $oldrev $newrev $refname” |column -t
    # can chain the pipe to post-receive hook here if needed

Leave a Comment

Your email address will not be published. Required fields are marked *