Skip to content
AstroPaper
Go back

How to use Git Hooks to set Created and Modified Dates

Updated:
Edit page

In this post I will explain how to use the pre-commit Git hook to automate the input of the created (pubDatetime) and modified (modDatetime) in the AstroPaper blog theme frontmatter

Table of contents

Open Table of contents

Have them Everywhere

Git hooks are great for automating tasks like adding or checking the branch name to your commit messages or stopping you committing plain text secrets. Their biggest flaw is that client-side hooks are per machine.

You can get around this by having a hooks directory and manually copy them to the .git/hooks directory or set up a symlink, but this all requires you to remember to set it up, and that is not something I am good at doing.

As this project uses npm, we are able to make use of a package called Husky (this is already installed in AstroPaper) to automatically install the hooks for us.

Update! In AstroPaper v4.3.0, the pre-commit hook has been removed in favor of GitHub Actions. However, you can easily install Husky yourself.

The Hook

As we want this hook to run as we commit the code to update the dates and then have that as part of our change we are going to use the pre-commit hook. This has already been set up by this AstroPaper project, but if it hadn’t, you would run npx husky add .husky/pre-commit 'echo "This is our new pre-commit hook"'.

Table of contents


Edit page
Share this post on:

Previous Post
AstroPaper 5.0
Next Post
AstroPaper 4.0