Git Pre Commit Hook Example Tutorial Best Practices

Logan Jul 13, 2026

Git pre-commit hooks are scripts that Git automatically runs at certain stages of the commit process. They're incredibly useful for ensuring code quality, enforcing coding standards, and automating tasks like running tests or updating changelog files. In this comprehensive guide, we'll explore an example of a pre-commit hook that checks for Git's commit message format.

git commit Explained (Day 6 Git Guide)
git commit Explained (Day 6 Git Guide)

Before we dive into the example, let's understand why pre-commit hooks are essential. In a team environment, consistent commit messages help maintain a uniform project history. Pre-commit hooks ensure that all contributors adhere to the agreed-upon commit message format, making it easier to understand and navigate the project's commit history.

Git Workflow
Git Workflow

Setting up the Pre-Commit Hook

To create a pre-commit hook, you'll need to create a script in your project's .git/hooks directory. If the hooks directory doesn't exist, you can create it yourself or initialize a new Git repository to generate it.

the git diagram shows how to use it
the git diagram shows how to use it

In this example, we'll create a JavaScript-powered pre-commit hook using Node.js and npm (Node Package Manager). If you're not familiar with Node.js, it's a JavaScript runtime environment that allows running JavaScript on the server. For this hook, we'll leverage the simple-git library to interact with the Git repository.

Installing Node.js and Setting up the Project

Git
Git

First, ensure Node.js is installed on your system. If not, download and install it from the official Node.js website. After installing Node.js, you can use npm (node --version and npm --version) to manage packages and dependencies.

Once Node.js is set up, create a new project directory for your pre-commit hook:

  1. Open your terminal or command prompt.
  2. Navigate to the desired project directory using the cd command.
  3. Initialize a new npm project: npm init -y
the git workflow and git commands poster
the git workflow and git commands poster

Creating the Pre-Commit Hook Script

Now that your project is initialized, let's create the pre-commit hook script. We'll write a JavaScript file named pre-commit.js and install the simple-git library as a dependency.

  1. Create a new file named pre-commit.js in your project directory.
  2. Open the file in your favorite code editor or IDE.
  3. Add the following code to the file:
a diagram showing the different types of objects in an object that are connected to each other
a diagram showing the different types of objects in an object that are connected to each other

```javascript const simpleGit = require('simple-git')(); const { exit } = require('process'); async function run() { const { messages } = await simpleGit.log([ '--pretty=format:%s', '--numstat=yes', '-1', ]); if (!messages) { console.log('No commit message found.'); exit(1); } const [message] = messages; const regex = /^([\w-]+):\s*(.+)$/; if (!message.match(regex)) { console.log('Invalid commit message format. Please use the following format:\n\n[Type]: Summary\n\nDescription.'); exit(1); } console.log(`Commit message is valid: ${message}`); } run().catch(console.error); ```

The script above checks the latest commit message's format. If the message doesn't match the expected format of [Type]: Summary (e.g., feat: Add login functionality), the script will exit with a non-zero status, preventing the commit from being made.

After creating the script, install the required dependencies:

Nitin Gupta (@niting786) / X
Nitin Gupta (@niting786) / X
Git Basic Commands
Git Basic Commands
Git Commands & Operatio ns
Git Commands & Operatio ns
a diagram that shows the flow of people in a network, and how to use it
a diagram that shows the flow of people in a network, and how to use it
Top 10 git commands you must know
Top 10 git commands you must know
Git workflow with example
Git workflow with example
Git Workflow Roadmap
Git Workflow Roadmap
the most used git commands in web design info graphic by admink com
the most used git commands in web design info graphic by admink com
git log Explained (Day 8 Git Guide)
git log Explained (Day 8 Git Guide)
Git & GitHub Commands Cheat Sheet 💻 | Essential Developer Guide
Git & GitHub Commands Cheat Sheet 💻 | Essential Developer Guide
a computer screen with the text stop forgeting git
a computer screen with the text stop forgeting git
Brij kishore Pandey on LinkedIn: Getting Started with Git- 𝗴𝗶𝘁 𝗶𝗻𝗶𝘁 : This is the very first… | 177 comments
Brij kishore Pandey on LinkedIn: Getting Started with Git- 𝗴𝗶𝘁 𝗶𝗻𝗶𝘁 : This is the very first… | 177 comments
the back cover of curiosity hooks, which features an image of a person's head
the back cover of curiosity hooks, which features an image of a person's head
Create a Git Commit: Mastering Version Control with Git
Create a Git Commit: Mastering Version Control with Git
Git Cheat Sheet for Everyday Work: Only the Commands You Need
Git Cheat Sheet for Everyday Work: Only the Commands You Need
Git Push Command
Git Push Command
GitHub Complete Beginner Guide (2026) | Learn Git & GitHub Step-by-Step
GitHub Complete Beginner Guide (2026) | Learn Git & GitHub Step-by-Step
Essential Git Commands: Your Go-To Cheat Sheet
Essential Git Commands: Your Go-To Cheat Sheet
See George Slanskiy’s activity on LinkedIn
See George Slanskiy’s activity on LinkedIn
the cover of 50 hooks every creator should steal before instagrams reach again save this
the cover of 50 hooks every creator should steal before instagrams reach again save this
  1. Open your terminal or command prompt.
  2. Navigate to your project directory.
  3. Run npm init -y to create a package.json file if you haven't already.
  4. Run npm install simple-git to install the simple-git library.

Configuring Git to Use the Pre-Commit Hook

Now that we've created the pre-commit hook script, let's tell Git to use it.

Making the Script Executable

For Git to recognize the script as a pre-commit hook, it must have executable permissions. You can set the permissions using the following command:

  1. Open your terminal or command prompt.
  2. Navigate to your project directory.
  3. Make the pre-commit.js script executable: chmod +x pre-commit.js

Moving the Script to the Git Hooks Directory

Finally, move the script to the .git/hooks directory. If the hooks directory doesn't exist, create it first.

  1. Create the hooks directory if it doesn't exist: mkdir -p .git/hooks
  2. Move the pre-commit.js script to the .git/hooks directory: mv pre-commit.js .git/hooks/pre-commit

The pre-commit hook is now set up and enabled. It will run automatically whenever you try to commit changes, enforcing the specified commit message format.

Testing the Pre-Commit Hook

Now that the pre-commit hook is configured, let's test it by making a commit with an invalid message format. If you've set up the hook correctly, Git should prevent the commit from being made until you provide a valid commit message.

Creating a New Commit with an Invalid Message

Try to commit changes with an invalid commit message format:

  1. Open your terminal or command prompt.
  2. Navigate to your project directory.
  3. Stage a file for commit: git add filename.js
  4. Attempt to commit with an invalid message format: git commit -m 'Invalid commit message format'

The pre-commit hook should prevent the commit, displaying the error message: "Invalid commit message format. Please use the following format:

[Type]: Summary

Description.

Now, try committing with a valid message format:

  1. Stage a file for commit: git add filename.js
  2. Commit with a valid message format: git commit -m "feat: Addition of login functionality"

The commit should succeed this time, demonstrating that the pre-commit hook is working as expected.

In this example, we explored how to create a pre-commit hook script that enforces a specific commit message format. This is just one of many use cases for pre-commit hooks. You can adapt this script to suit your team's specific needs or automate other tasks, such as running tests or updating changelog files.

Happy coding, and may your pre-commit hooks serve you well!