Pre-commit hooks in Git, an essential feature that helps maintain code quality, enforce standards, and automate checks before code changes are committed.
Git hooks are custom scripts that Git can run automatically at different points in your development process. They allow you to automate and enforce various checks, making them extremely helpful in maintaining high code quality and consistency.
Git hooks are stored in the .git/hooks
directory within each Git repository. They can be set to trigger specific scripts at various stages, such as before committing or after pushing code.
The pre-commit hook is a client-side hook that runs automatically before Git commits any changes to the repository. It’s the first in the series of commit-related hooks and is ideal for tasks like:
A pre-commit hook allows you to enforce coding standards and prevent issues before they enter the codebase, ensuring that only high-quality code makes it into your project.
Using pre-commit hooks offers several advantages:
Navigate to the .git/hooks Directory: Each Git repository has a .git/hooks
directory. This directory contains example hooks with .sample
extensions.
Create a Pre-Commit Hook File: Rename or create a new file called pre-commit
in the .git/hooks
directory without any extension.
Make the Hook Executable: Run the following command to ensure the file has executable permissions:
chmod +x .git/hooks/pre-commit
Now, Git will automatically execute the pre-commit
script whenever you commit changes.
Let’s start with a simple example of a pre-commit hook.
Here’s a basic pre-commit hook that prevents commits with empty messages:
#!/bin/bash
# Check if commit message is empty
if [ -z "$(git log -1 --pretty=%B)" ]; then
echo "Error: Commit message cannot be empty."
exit 1
fi
Explanation: This script checks if the commit message is empty. If it is, it outputs an error and prevents the commit from proceeding.
Error: Commit message cannot be empty.
This simple hook ensures that all commits have meaningful messages, which improves the readability of your Git history.
For more advanced hooks, you can add logic to check code quality, run unit tests, and prevent accidental commits of sensitive information.
Let’s create a pre-commit hook that ensures all tests pass before committing code.
#!/bin/bash
# Run tests
npm test
RESULT=$?
if [ $RESULT -ne 0 ]; then
echo "Error: Tests failed. Please fix issues before committing."
exit 1
fi
Explanation: This script runs npm test
, which executes the tests defined in your project. If the tests fail, the script outputs an error and stops the commit.
Error: Tests failed. Please fix issues before committing.
This ensures that only code that passes all tests can be committed, helping to maintain a stable codebase.
Linting checks for coding errors and style issues. Let’s add ESLint as a pre-commit hook to automatically check for issues in JavaScript code.
#!/bin/bash
# Run ESLint
npx eslint .
if [ $? -ne 0 ]; then
echo "Error: Linting failed. Please fix issues before committing."
exit 1
fi
Explanation: This script runs npx eslint .
to lint the codebase. If there are any linting errors, it outputs an error message and prevents the commit.
Error: Linting failed. Please fix issues before committing.
Using linting in pre-commit hooks enforces code standards and helps maintain a consistent code style across the project.
Running tests in the pre-commit hook is beneficial to ensure that changes do not introduce bugs. However, it’s essential to keep the test suite efficient to avoid delays.
You can modify the hook to run only a specific subset of tests, especially if your project has a large test suite.
#!/bin/bash
# Run selective tests
npm test -- --testPathPattern="src/tests"
RESULT=$?
if [ $RESULT -ne 0 ]; then
echo "Error: Some tests failed. Please fix them before committing."
exit 1
fi
In addition to ESLint, you can integrate other tools like Prettier to automatically format code before committing.
#!/bin/bash
# Format code with Prettier
npx prettier --write .
# Add changes to staging
git add .
echo "Code formatted successfully."
Explanation: This script runs Prettier to format the code and stages the formatted files. This ensures consistent code formatting across the project.
Code formatted successfully.
chmod +x
)..git/hooks
directory and is named pre-commit
.To temporarily skip the pre-commit hook, you can use the --no-verify
flag:
git commit -m "Your message" --no-verify
This bypasses the hook, allowing you to commit changes without running the script.
Pre-commit hooks are essential tools in modern Git workflows, helping to automate tasks and enforce quality standards. By integrating pre-commit hooks, you ensure that only well-tested, properly formatted, and high-quality code is added to the project. Happy Coding!❤️