Skip to content
Go back

How to Use Git Hooks to Auto-Set Post Dates

How to use Git hooks to automatically set created and modified dates in astro-minimax.

The frontmatter of astro-minimax blog posts includes pubDatetime (publication date) and modDatetime (modification date) fields. Manually maintaining these dates is tedious and easy to forget. This article explains how to handle this automatically with Git hooks.

Important Note

The hook only auto-fills empty date fields. It will NOT overwrite values you’ve manually specified.

If you’ve already set pubDatetime or modDatetime in your frontmatter, the hook preserves your values.

The astro-minimax CLI provides a hooks command that automatically installs Husky and configures the pre-commit hook:

# Run from anywhere in your blog project (supports subdirectories)
astro-minimax hooks install
bash

This will:

  1. Detect project type (single project / Monorepo)
  2. Install Husky as a dev dependency
  3. Create .husky/pre-commit hook script
  4. Configure the prepare script

After installation, every git commit will auto-fill empty date fields:

ScenarioConditionBehavior
New postpubDatetime is emptyAuto-fill with current time
New postpubDatetime has valueSkip, keep original value
Modified postdraft: false + modDatetime emptyAuto-fill with current time
Modified postdraft: false + modDatetime has valueSkip, keep original value
First publishdraft: firstChange to draft: false, clear modDatetime

Other commands:

astro-minimax hooks status     # Show current status
astro-minimax hooks uninstall  # Remove hooks
bash

Option 2: Manual Configuration

If you prefer to configure it yourself, follow these steps.

Step 1: Install Husky

Husky is a Git hook management tool:

pnpm add -D husky
npx husky init
bash

Step 2: Create the pre-commit Hook

Edit .husky/pre-commit:

Hook Logic Explained

New files (A):

  1. Check if pubDatetime is empty
  2. If empty, fill with current time; if has value, skip

Modified files (M):

  1. Check draft status
  2. If draft: false: check if modDatetime is empty, fill if empty
  3. If draft: first: change to draft: false, clear modDatetime

First Publish Workflow

Use draft: first for automated first-time publishing:

---
title: "New Post"
pubDatetime:           # Leave empty, hook will auto-fill
modDatetime:           # Leave empty
draft: first           # First publish marker
---
yaml

On commit, the hook will:

  1. Auto-fill pubDatetime
  2. Change draft to false
  3. Future modifications will auto-update modDatetime

Notes

  1. Git hooks are local only — Team members need to run astro-minimax hooks install individually
  2. Files must be staged first — The hook runs on git commit and processes git added files
  3. Monorepo support — CLI automatically detects git root and installs hooks in the correct location
  4. Manually specified dates are preserved — The hook only fills empty values


Previous Post
Complete Setup Guide: From Zero to Production
Next Post
Setup Umami Analytics: Free Vercel Deployment

评论区

文明评论,共建和谐社区