How to Install GIT Hooks

Git hooks are powerful automation tools that allow developers to execute custom scripts at different points in the Git workflow, such as before committing, after merging, before pushing, and more. These hooks help enforce coding standards, run tests, and ensure consistency across a project. This guide will walk you through installing Git hooks, configuring them, and using them effectively.

📌 Table of Contents

  • What Are Git Hooks?
  • Types of Git Hooks
  • Installing Git Hooks
  • How to Enable and Use Git Hooks
  • Example: Installing a Pre-Commit Hook
  • Managing Git Hooks with Husky
  • Installing Git Hooks Automatically
  • Checking Installed Git Hooks
  • Best Practices for Using Git Hooks
  • Troubleshooting Git Hooks
  • Conclusion

What Are Git Hooks?

Git hooks are executable scripts that trigger actions based on Git events. They help automate repetitive tasks and ensure team-wide compliance with coding practices. Hooks can be categorized into:

  • Client-side hooks: Run on a developer’s local machine, triggering before commits, merges, or updates.
  • Server-side hooks: Execute on the remote repository when a developer pushes changes.

Git hooks reside in the .git/hooks directory of each repository.

Types of Git Hooks

Git provides multiple types of hooks, but the most commonly used ones include:

Client-Side Hooks

Hook Name Purpose
pre-commit Runs before a commit is finalized (e.g., for linting, running tests).
commit-msg Validates commit messages to enforce a specific format.
pre-rebase Executes before a rebase operation starts.
post-checkout Runs after git checkout, useful for setting up dependencies.
post-merge Triggers after a successful git merge.

Server-Side Hooks

Hook Name Purpose
pre-receive Runs before processing a git push on the server.
update Executes when a branch reference is updated on the server.
post-receive Runs after the push is accepted, often used for notifications.

How to Install GIT Hooks

To install Git hooks, follow these steps:

Step 1: Navigate to Your Repository’s Hooks Directory

First, navigate to the Git hooks directory in your local repository:

cd your-repo/.git/hooks

Step 2: List Available Sample Hooks

Run the following command to list available hooks:

ls -l

You will see multiple sample hooks ending with .sample.

Step 3: Rename the Sample Hook to Activate It

To activate a hook, remove the .sample extension. For example, to enable the pre-commit hook:

mv pre-commit.sample pre-commit

Step 4: Make the Hook Executable

To allow the script to run, set executable permissions:

chmod +x pre-commit

How to Enable and Use Git Hooks

  1. Open the hook file in a text editor (e.g., Vim, Nano, VS Code).
    nano pre-commit
    
  2. Write a simple hook script. For example, the following script prevents commits that contain the word “WIP” (work in progress):
    #!/bin/sh
    if git diff --cached | grep -q "WIP"; then
        echo "🚨 ERROR: Your commit contains WIP code. Please remove it before committing."
        exit 1
    fi
    
  3. Save the file and exit.

Now, when you try to commit a file with “WIP” in the code, Git will prevent the commit.


Example: Installing a Pre-Commit Hook

A common use case is enforcing coding standards with Prettier before committing.

  1. Install Prettier (if not already installed):
    npm install --save-dev prettier
    
  2. Create the .git/hooks/pre-commit file:
    touch .git/hooks/pre-commit
    
  3. Add the following script inside pre-commit:
    #!/bin/sh
    npx prettier --write "**/*.{js,jsx,ts,tsx,json,css,md}"
    git add .
    
  4. Save the file and make it executable:
    chmod +x .git/hooks/pre-commit
    

Now, every time you commit, Prettier will automatically format your code.


Managing Git Hooks with Husky

Husky is a popular tool for managing Git hooks in JavaScript projects.

Installing Husky

  1. Install Husky:
    npm install husky --save-dev
    
  2. Enable Git hooks:
    npx husky install
    
  3. Add a pre-commit hook:
    npx husky add .husky/pre-commit "npm test"
    git add .husky/pre-commit
    

Installing Git Hooks Automatically

To install hooks automatically when cloning a repository, add a hooks directory to your repo and configure Git to use it globally:

git config --global core.hooksPath hooks

Checking Installed Git Hooks

To verify if hooks are installed, run:

ls .git/hooks/

For Husky-managed hooks:

npx husky list

Best Practices for Using Git Hooks

Keep hooks fast: Slow hooks reduce productivity.
Use hooks sparingly: Avoid unnecessary automation.
Use version control: Store hooks in the repository for consistency.
Document hooks: Provide usage instructions in README.md.


Troubleshooting Git Hooks

Common Issues and Fixes

Hooks not executing? Ensure the script is executable:

chmod +x .git/hooks/pre-commit

Getting permission denied? Run:

sudo chmod +x .git/hooks/*

Hooks not running with Husky? Reinstall:

npx husky install

Conclusion

Git hooks are an essential tool for automating Git workflows, ensuring code quality, and enforcing best practices. Whether you’re using built-in Git hooks, manually configuring scripts, or leveraging Husky for easier management, these automation tools can save time and improve project consistency.

For more details, visit the official Git documentation:
🔗 Git Hooks Documentation
🔗 Husky Git Hooks

Learn more about how to merge specific commit in git​

 

Related articles

How to Create a Virtual Machine in Azure

How to Create a Virtual Machine in Azure: Step-by-Step Guide Introduction to Azure Virtual Machines Microsoft Azure provides cloud-based virtual...

CI/CD in AWS | CodePipeline | CodeBuild | CodeDeploy

🚀CI/CD in AWS Using CodePipeline, CodeBuild, and CodeDeploy Building and deploying software efficiently is a cornerstone of modern development...

Top 50 Essential Ubuntu Commands for Daily Use: Detailed Guide with Examples

Top 50 Essential Ubuntu Commands for Daily Use: Detailed Guide with Examples File and Directory Management ls - List...

Azure Cost Optimization

Azure Cost Optimization Introduction Azure Virtual Machines (VMs) provide scalable and flexible compute resources in the cloud, enabling businesses to...