Adding a script to run when commiting changes on Git

2024-02-02

A while ago, I showed the script used to create this blog from templates. I just figured out this week how to automatically run it when commiting a new blog post, and this post is basically a test of this method.

So, as those of you who uses git already knows, a repository contains a hidden folder called .git. In this folder there is a subfolder called hooks, which is already prepopulated with example scripts with extensions .sample.

In our case, we need a script that is run when commiting (I think?). For this the script needs to be called pre-commit, like the example file pre-commit.sample but without the extension. So in our case, as the script I want to run is a R script called process_blog.R, the file should simply contains:

#!/bin/sh
Rscript blog/process_blog.R

I will need to be then given permission to execute:

chmod +x .git/hooks/pre-commit

And voilà!