Chuck's Academy

Git Hooks and Automation

Practical Examples of Pre-commit Hooks

In this section, we will explore some practical examples of how to use the pre-commit hook to automate important tasks before allowing a commit to the repository. These examples will help you maintain code quality, avoid common errors, and ensure a more efficient workflow.

Example 1: Code Formatting Verification

Consistent code formatting is crucial for project maintenance and readability. We will configure a pre-commit hook to verify code formatting using Prettier, a code formatting tool.

Hook Configuration

  1. Install Prettier in your project:

    bash
  2. Create the file .git/hooks/pre-commit with the following content:

    bash
  3. Make the script executable:

    bash

[Placeholder for image: Screenshot of pre-commit script in a text editor, showing Prettier configuration]

Example 2: Running Unit Tests

Unit tests ensure that code changes do not break existing functionality. We will configure a pre-commit hook to run unit tests using Jest.

Hook Configuration

  1. Install Jest in your project:

    bash
  2. Create the file .git/hooks/pre-commit with the following content:

    bash
  3. Make the script executable:

    bash

Example 3: Verifying Commit Messages

Ensuring that commit messages are descriptive and follow a specific format can help with code review and change tracking. We will configure a pre-commit hook to verify commit message format.

Hook Configuration

  1. Create the file .git/hooks/commit-msg with the following content:

    bash
  2. Make the script executable:

    bash

Example 4: Static Code Analysis with ESLint

Static code analysis can help identify errors, style issues, and other potential problems before the code is merged into the main codebase. We will configure a pre-commit hook to run ESLint.

Hook Configuration

  1. Install ESLint in your project:

    bash
  2. Configure ESLint by creating the .eslintrc.json file if you don't have one yet:

    json
  3. Create the file .git/hooks/pre-commit with the following content:

    bash
  4. Make the script executable:

    bash

Example 5: Preventing Temporary Files in Commits

It's common for temporary or backup files to be generated during development. Ensuring these files are not included in commits helps keep the repository clean.

Hook Configuration

  1. Create the file .git/hooks/pre-commit with the following content:

    bash
  2. Make the script executable:

    bash

Customization

Remember, you can combine several tasks in a single pre-commit hook according to your project's needs. This flexibility will allow you to maintain an efficient workflow and ensure code quality.

Combined Example

You can combine the previous examples into a single script:

bash

Remember to always make the script executable:

bash

In the next section, we will see practical examples of pre-push hooks and how they can help ensure code quality before it is pushed to the remote repository.

Let's continue!


Ask me anything