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.

Leave a Comment

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