Git Automation With Hooks


I have been using static site generator's for my wiki and blog for some time now. I also utilize git with them for version control. I finally took the time to learn a little bit about git hooks in order to automate the publishing of the content on Git push requests to the Git server. I wanted to do this because I have been trying out different editors and the workflow before would require me to ssh into the VM that is responsible for publishing the sites and initiating the publish. The new system will automatically publish when I push these changes. It's pretty simple stuff and much of the work was done but it was one of those things I put off for some reason.

Git Hooks

Git has hooks. These hooks can be executed at various points during a Git interaction. The server side hooks live in the git repository in the hooks directory. In my case I needed a post-receive hook. This is a bash script that is executed after a change is pushed to the repo from a client. Any time I push commits from a remote machine to the origin main repo this script get's executed.

The Script

All that really needs to happen is to have git initiate an SSH connection to my publishing VM and execute the commands I was previously executing. To simplify things further, I created a script on the publishing VM to execute from the Git server.


while read oldrev newrev ref
    if [[ $ref =~ .*/main$ ]]; then
        ssh "/home/pat/"
  • Stores some info provided by Git to stdin into variables
  • If the push was to main run a script on the generator VM

Now anytime I push to my git server from a remote client the changes get published automatically. I should probably create a branch specific to publishing or something like that to make sure that the publish side of things stays clean.

The commands for my blog were even simpler so I just execute them directly from the hook script rather then a second bash script.

OK, now I need to find a spell-checker for vscodium so I can check this post for errors.

Wrap Up

I should have taken care of this a long time ago. Pushing these changes manually wasn't a big deal but after you haven't done it for 3 months and you can't really remember what needs to be done it turned into a barrier for making blog posts. I am glad it's done and gives me one more tool in the box. Hopefully now I will get back to blogging a little more.


When I committed this post, the publish failed. I am going to try again with this update but it looks like it may have just been a temporary issue with my shared hosting, which could happen from time to time. This is a reminder that if this were production putting a more robust solution in place that would check for success and retry or notify upon failure would be necessary.