Git hooks are scripts that Git executes automatically before or after specific events, such as committing, pushing, or merging. They allow you to automate tasks, enforce policies, and ensure consistency in your development workflow. Git hooks are stored in the .git/hooks directory of your repository and can be written in any scripting language. Below, we explain what Git hooks are, how they work, and provide examples of how to use them.

Types of Git Hooks

Git hooks are categorized into client-side and server-side hooks. Client-side hooks run on your local machine, while server-side hooks run on the remote repository. Here are some commonly used hooks:

Client-Side Hooks

  • pre-commit: Runs before a commit is created. It can be used to check code style, run tests, or verify commit messages.
  • commit-msg: Runs after the commit message is created but before the commit is finalized. It can validate the commit message format.
  • pre-push: Runs before a push is executed. It can be used to run tests or check for errors before pushing changes to the remote repository.

Server-Side Hooks

  • pre-receive: Runs before changes are accepted by the remote repository. It can enforce policies or reject changes that don’t meet certain criteria.
  • post-receive: Runs after changes are accepted by the remote repository. It can trigger notifications or deployment scripts.

How to Use Git Hooks

Git hooks are stored in the .git/hooks directory of your repository. Each hook is a script with a specific name (e.g., pre-commit, commit-msg). To enable a hook, create a script with the appropriate name and make it executable.

Example: Create a Pre-Commit Hook

Let’s create a pre-commit hook that runs a linter to check code style before allowing a commit.

  1. Navigate to the hooks directory:

  2. cd .git/hooks
  3. Create the pre-commit script:

  4. touch pre-commit
    chmod +x pre-commit
  5. Add the following content to the script:

  6. #!/bin/sh
    # Run a linter to check code style
    if ! npm run lint; then
    echo "Linting failed. Commit aborted."
    exit 1
    fi

This script runs the npm run lint command. If the linter fails, the commit is aborted.

Example: Create a Commit-Msg Hook

Let’s create a commit-msg hook that enforces a specific commit message format.

  1. Navigate to the hooks directory:

  2. cd .git/hooks
  3. Create the commit-msg script:

  4. touch commit-msg
    chmod +x commit-msg
  5. Add the following content to the script:

  6. #!/bin/sh
    # Enforce a commit message format
    if ! grep -qE "^(feat|fix|docs|style|refactor|test|chore): " "$1"; then
    echo "Commit message must start with 'feat:', 'fix:', 'docs:', 'style:', 'refactor:', 'test:', or 'chore:'."
    exit 1
    fi

This script checks if the commit message starts with one of the specified prefixes. If not , it aborts the commit and displays an error message.

Testing Your Hooks

After creating your hooks, you can test them by attempting to commit changes. If the conditions specified in your hooks are not met, the commit will be aborted, and you will see the corresponding error message.

Conclusion

Git hooks are a powerful feature that can help automate tasks and enforce coding standards in your development workflow. By using hooks like pre-commit and commit-msg, you can ensure that your code meets certain criteria before it is committed or pushed. This not only improves code quality but also enhances collaboration among team members.