ApiaryActive
Try: pause · settings · learn · wipe
← Community / Reading Room
GH
git · 3 min read

git hooks pre commit

======================================================

======================================================

As a developer, you're likely no stranger to the importance of maintaining clean and organized codebases. One crucial step in this process is ensuring your code adheres to specific standards before committing it. Enter git hooks, specifically the pre-commit hook, which allows you to enforce these standards by running custom scripts during the commit process.

The Technique

The pre-commit hook is a Git hook that runs automatically when you attempt to commit changes. Its primary purpose is to validate your code before allowing it to be committed. This can include tasks such as:

  • Linting: Checking for coding standards, syntax errors, and potential issues in the code.
  • Formatting: Enforcing consistent formatting and style throughout the project.
  • Validation: Verifying that certain conditions are met before committing, like checking if all dependencies are installed.

To set up a pre-commit hook, you'll typically create a script in your Git repository's .git/hooks/pre-commit directory. This script will contain any custom commands or tools to be executed during the commit process.

Concrete Examples

Let's dive into some concrete examples of how you can utilize the pre-commit hook:

Example 1: Linting with ESLint

Suppose you're working on a TypeScript project and want to enforce strict coding standards. You can use ESLint, a popular linter for JavaScript and TypeScript.

// .git/hooks/pre-commit (create or modify)
#!/bin/sh

# Check if there are any linting errors
npm run eslint src/
if [ $? -ne 0 ]; then
    echo "Linting failed. Please fix the issues."
    exit 1
fi

In this example, we're running ESLint on the src/ directory and checking its return code. If there are any linting errors, the commit will be aborted.

Example 2: Formatting with Prettier

Another common task is ensuring consistent formatting throughout your project. You can use Prettier to achieve this.

# .git/hooks/pre-commit (create or modify)
#!/bin/sh

# Check if there are any formatting issues
npm run prettier src/
if [ $? -ne 0 ]; then
    echo "Formatting failed. Please fix the issues."
    exit 1
fi

Similar to the ESLint example, we're running Prettier on the src/ directory and checking its return code.

Example 3: Validation with husky

Husky is a popular package for managing Git hooks in your project. You can use it to set up more complex validation scripts.

// package.json (add)
"husky": {
    "hooks": {
        "pre-commit": "npm run validate"
    }
},
"scripts": {
    "validate": "jest --coverage"
}

In this example, we're using Husky to run a custom script (validate) during the commit process. This script will execute Jest with code coverage.

When NOT to Use It

While pre-commit hooks are incredibly useful, there are situations where you might want to avoid using them:

  • Overly complex scripts: If your hook is too long or complicated, it may slow down your commit process.
  • Inconsistent formatting: Enforcing strict formatting can be a double-edged sword. Make sure you're not introducing inconsistencies or conflicts with other tools.
  • Performance issues: Hooks that run expensive operations (e.g., rebuilding the entire project) might cause performance issues.

Related Apiary Lessons

If you want to learn more about Git hooks and related topics, check out these additional lessons:

Conclusion

The pre-commit hook is a powerful tool for maintaining clean, organized codebases. By running custom scripts during the commit process, you can ensure your code adheres to specific standards. Remember to balance enforcement with flexibility and performance considerations.

As we wrap up this lesson on Git hooks pre commit, remember: "A well-organized hive is a happy hive."

Frequently asked
What is git hooks pre commit about?
======================================================
What should you know about the Technique?
The pre-commit hook is a Git hook that runs automatically when you attempt to commit changes. Its primary purpose is to validate your code before allowing it to be committed. This can include tasks such as:
What should you know about concrete Examples?
Let's dive into some concrete examples of how you can utilize the pre-commit hook:
What should you know about when NOT to Use It?
While pre-commit hooks are incredibly useful, there are situations where you might want to avoid using them:
What should you know about related Apiary Lessons?
If you want to learn more about Git hooks and related topics, check out these additional lessons:
References & sources
  1. Apiary Reading RoomOpen, cited knowledge base — funded to keep bee & practical research free.
From the Apiary Reading Room. Opinion & editorial — not financial advice. We don't overclaim.
More from the Reading Room