Git hooks are scripts that Git executes before or after events such as commit, push, and receive. They allow you to automate tasks and enforce policies in your Git workflow. This section will cover the basics of Git hooks, how to set them up, and provide practical examples.

What are Git Hooks?

Git hooks are custom scripts that run automatically when certain events occur in a Git repository. They can be used to:

  • Enforce coding standards
  • Run tests before committing code
  • Automatically format code
  • Notify team members of changes

Types of Git Hooks

Git hooks are divided into two main categories:

  1. Client-Side Hooks: These hooks are triggered by operations such as committing and merging. They are stored in the .git/hooks directory of your local repository.
  2. Server-Side Hooks: These hooks are triggered by network operations such as receiving pushed commits. They are stored in the hooks directory of the Git repository on the server.

Common Client-Side Hooks

Hook Name Trigger Event Description
pre-commit Before a commit is made Used to inspect the snapshot before committing.
prepare-commit-msg Before the commit message editor is fired up Used to edit the default commit message.
commit-msg After the commit message is created Used to validate or modify the commit message.
post-commit After a commit is made Used to perform actions after a commit, such as notifications.
pre-push Before a push is made Used to run tests or checks before pushing changes to a remote repository.

Common Server-Side Hooks

Hook Name Trigger Event Description
pre-receive Before changes are accepted Used to enforce policies before changes are accepted.
update Before a branch is updated Used to enforce policies on a per-branch basis.
post-receive After changes are accepted Used to perform actions after changes are accepted, such as deployment.

Setting Up Git Hooks

To set up a Git hook, follow these steps:

  1. Navigate to the .git/hooks directory in your repository.
  2. Create a new file with the name of the hook you want to set up (e.g., pre-commit).
  3. Make the file executable by running chmod +x <hook-name>.
  4. Write your script in the file.

Example: Pre-Commit Hook

Let's create a simple pre-commit hook that checks for TODO comments in the code before allowing a commit.

  1. Navigate to the .git/hooks directory:

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

    touch pre-commit
    
  3. Make the file executable:

    chmod +x pre-commit
    
  4. Add the following script to the pre-commit file:

    #!/bin/sh
    # Check for TODO comments
    if grep -r "TODO" .; then
        echo "Commit rejected: TODO comments found."
        exit 1
    fi
    

This script will search for "TODO" comments in the code and reject the commit if any are found.

Practical Example: Running Tests Before Commit

Another common use case for Git hooks is to run tests before allowing a commit. This ensures that only code that passes tests is committed to the repository.

  1. Navigate to the .git/hooks directory:

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

    touch pre-commit
    
  3. Make the file executable:

    chmod +x pre-commit
    
  4. Add the following script to the pre-commit file:

    #!/bin/sh
    # Run tests
    npm test
    if [ $? -ne 0 ]; then
        echo "Commit rejected: Tests failed."
        exit 1
    fi
    

This script will run the npm test command before allowing a commit. If the tests fail, the commit will be rejected.

Exercises

Exercise 1: Create a Pre-Commit Hook

Task: Create a pre-commit hook that checks for trailing whitespace in the code and rejects the commit if any is found.

Solution:

  1. Navigate to the .git/hooks directory:

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

    touch pre-commit
    
  3. Make the file executable:

    chmod +x pre-commit
    
  4. Add the following script to the pre-commit file:

    #!/bin/sh
    # Check for trailing whitespace
    if grep -r "[[:blank:]]$" .; then
        echo "Commit rejected: Trailing whitespace found."
        exit 1
    fi
    

Exercise 2: Create a Post-Commit Hook

Task: Create a post-commit hook that prints a message to the console after a commit is made.

Solution:

  1. Navigate to the .git/hooks directory:

    cd .git/hooks
    
  2. Create the post-commit file:

    touch post-commit
    
  3. Make the file executable:

    chmod +x post-commit
    
  4. Add the following script to the post-commit file:

    #!/bin/sh
    # Print a message after commit
    echo "Commit successful!"
    

Summary

In this section, we covered the basics of Git hooks, including what they are, the different types, and how to set them up. We also provided practical examples and exercises to help you get started with using Git hooks in your workflow. By leveraging Git hooks, you can automate tasks, enforce policies, and improve the overall quality of your codebase.

© Copyright 2024. All rights reserved