Skip to content

maddisanotoole/git-hook-examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Examples of Git Hooks

Overview

Git hooks are Git's in-built method of triggering scripts based on actions. This repo's purpose is to provide an overview of the different types of git hooks and example use cases.

Table of Contents

  1. Implementation
  2. Different Types of Hooks
  3. Committing Hooks
  4. Manually tracking and installing hooks
  5. Using a Git Hook Manager (Husky)
  6. Resources

Implementation

Hooks are stored in .git/hooks folder. By default .git/hooks will be populated with example hooks, if you want to enable them:

  1. Remove the ".sample" suffix from the file name
  2. Make the hook executable using: chmod +x .git/hooks/hook-name
  3. Run npm i to install them

Different Types of Hooks

When choosing a Git hook, consider:

  • How frequently the hook will be triggered
  • How long the hook's operations will take to run
  • The impact on developer workflow
  • Whether the operation needs to run on every commit or can wait until push

Client side hooks are for operations that run on your local device (e.g. commit, rebase). Server side hooks are for network operations that interact with the remote Git server (e.g. push).

Name Type Trigger
pre-commit client-side git commit
commit-msg client-side git commit
pre-push client-side git push
pre-rebase client-side git rebase
pre-merge-commit server-side before merge
post-merge server-side after merge
pre-receive server-side before push to remote
update server-side after push to remote
post-receive server-side after push to remote
post-update server-side after push to remote
pre-applypatch client-side git am
applypatch-msg client-side git am
post-applypatch client-side git am
pre-commit client-side git commit
prepare-commit-msg client-side git commit
post-commit client-side git commit
pre-auto-gc client-side git gc
post-rewrite client-side git rebase/amend
pre-push client-side git push
pre-push client-side git push

Client-Side Hooks

pre-commit (Runs before every commit)

Analyses staged code. Best suited for quick checks that provide immediate feedback.

Example Use Cases:

  • blocking certain code (e.g console.log)
  • checking file size or lines of code exceeds limit
  • formatting code

prevent-console-logs: Checks if any staged files contain "console.log", if so, cancels the commit and tells the user what file and line the log is on.

pre-commit example - prevent console logs

limit-line-number: Checks if any staged files contain more than a specified line number, if so, cancels the commit and tells the user what files exceed the limit

pre-commit example - limit line number

commit-msg (Runs after commit message is entered)

Can change the contents of a commit message. Ideal for maintaining commit message standards.

Example Use Cases:

  • Adding ticket number to commit msg
  • Enforcing commit message format - blocking vague messages (e.g. fix)

include-ticket-number: Add ticket numbers to commit messages if they are missing, prevents commits to branches without ticket numbers in their name. This means direct commits to master will be prevented.

commit-msg example - include ticket number

[!NOTE] > pre-commit and commit-msg hooks can be bypassed with git commit --no-verify

pre-push (Runs before code is pushed to remote)

Looks at all commits since the last push. Best suited for time-consuming operations since pushing is less frequent than committing.

Example Use Cases:

  • Running comprehensive test suites
  • Checking for secrets or sensitive data
  • Running full codebase linting
  • Validating API documentation
  • Preventing commits that start with "WIP"

Server-Side Hooks

pre-merge-commit and post-merge (Run before/after merges)

These hooks are ideal for team-wide notifications and deployment preparations.

Example Use Cases:

  • Notifying team via Slack/Teams
  • Triggering CI/CD pipelines
  • Updating documentation
  • Running deployment checks

send-message-to-slack: Send messages to slack when certain actions happen (e.g. if a branch is merged). Pre-requisite set up a webhook for your Slack workspace. Update the example url in the script with your generated webhook url

Webhook slack message

Committing Hooks

Git hooks are ignored by default. If you want to commit them, you can do so manually or use a git hook manager:

Manually tracking and installing hooks

  1. Create a hooks/ directory in your repository to store the hooks:
    mkdir hooks
  2. Add hooks to version control:
     git add hooks/commit-msg
     git commit -m "Add commit-msg hook"
  3. Create a setup-hooks.sh script to install the hooks:
    #!/bin/bash
    cp hooks/* .git/hooks/
    chmod +x .git/hooks/*
    npm i
  4. Make the script executable:
    chmod +x setup-hooks.sh
  5. Run the script: bash ./setup-hooks.sh

    [!NOTE] Optional: Add a script to your package.json, so you can run it with npm run setup instead

    "scripts": {
      "setup": "./setup-hooks.sh"
    }

Using a Git Hook Manager (Husky)

For Node.js projects, you can use Husky to manage Git hooks more easily. Husky automatically sets up Git hooks when you install it and manages them through your package.json. To install and initialise husky in your project, run:

npm install husky --save-dev
npx husky install

After installation, you can store hooks in the .husky/hooks directory. They will be installed the next time you run npm i. You can commit husky files directly with no need to copy them to the .git/hooks folder.

If your project is not based on Node.js, most runtime environments have Git hook managers tailored for them. For example, in Kotlin projects, you can use libraries like as Ktlint or plugins like Gradle's git-hooks plugin, which provide similar functionalities for managing hooks.

Resources

About

Example use cases for git hooks

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages