Back to Git & Dev Tools
2025-12-055 min read

Pre-Commit (Git & Dev Tools)

Learn Pre-Commit (Git & Dev Tools) step by step with clear examples and exercises.

Title: Pre-Commit (Git & Dev Tools) - A full guide for Developers

Why This Matters

Pre-commit is a powerful tool that helps developers maintain high-quality codebases by automating code quality checks before each commit. It saves time, effort, and reduces the risk of introducing errors into your project. By enforcing coding standards and best practices, pre-commit ensures that all contributions to a project are consistent and efficient.

Prerequisites

To follow this guide, you should have a basic understanding of:

  1. Git version control system (Git Basics)
  2. Common coding practices and linters such as ESLint, Black, or Flake8 (ESLint, Black, Flake8)
  3. How to install and manage software packages using tools like pip or npm (pip, npm)
  4. Familiarity with your project's specific requirements and coding standards

Core Concept

Pre-commit is a Git hook that runs custom scripts or linters automatically before every commit. It allows you to define a set of checks for your codebase, ensuring that all contributions adhere to the project's coding standards and best practices.

Installing Pre-Commit

To install pre-commit, first create a virtual environment (if necessary) and install it using pip:

pip install virtualenv
virtualenv venv
source venv/bin/activate
pip install pre-commit

Next, navigate to your Git repository and initialize the pre-commit configuration file:

pre-commit init

Defining Pre-Commit Hooks

Pre-commit hooks are defined in a .pre-commit-config.yaml file at the root of your repository. This file contains a list of commands that will be run before each commit. Each command represents a check or linter that you want to enforce for your project.

Here's an example configuration file:

repos:
- repo: local
hooks:
- id: trailing-whitespace
name: Trailing Whitespace Checker
entry: trailing-whitespace
- id: black
name: Black Formatter
entry: black --line-length=88
- id: flake8
name: Flake8 Linter
entry: flake8 .

In this example, we've defined three hooks: a check for trailing whitespace, a formatting pass using the Black formatter with a line length of 88 characters, and a linting pass using Flake8.

Running Pre-Commit Hooks

Once you have your configuration file set up, pre-commit will automatically run the defined checks before each commit. If any of the checks fail, the commit will be aborted, allowing you to correct the issues before proceeding.

Worked Example

Let's walk through an example of using pre-commit with a JavaScript and Python project that requires both ESLint for JavaScript and Black for Python formatting.

  1. Install the necessary linters:
pip install black
npm install eslint --save-dev
  1. Configure ESLint in your .eslintrc.json file.
  1. Initialize pre-commit and add the hooks for Black and ESLint:
pre-commit init
echo "repos:\n - repo: .\n hooks:\n - id: black\n name: Black Formatter\n entry: black --line-length=88\n - id: eslint\n name: ESLint\n entry: npm run lint" >> .pre-commit-config.yaml

Now, every time you attempt to commit changes, pre-commit will run Black for your Python files and ESLint for your JavaScript files, ensuring that both are formatted correctly before the commit is allowed.

Common Mistakes

  1. Forgetting to initialize the .pre-commit-config.yaml file: Make sure you create this file at the root of your repository and add your hooks as described in the Core Concept section.
  2. Not specifying the correct entry for each hook: Ensure that the command for each hook correctly points to the appropriate linter or checker.
  3. Ignoring failed checks: If a commit fails due to pre-commit checks, address the issues and try committing again. Ignoring failed checks can lead to inconsistencies in your codebase over time.
  4. Overlooking project-specific requirements: Customize your pre-commit configuration file to meet the specific needs of your project, such as additional linters or custom scripts.
  5. Assuming that pre-commit will automatically fix issues: While some linters may offer autofix options, it's essential to review and understand the changes made by the linter before committing them.
  6. Not updating the configuration file when adding new files or directories: Ensure that your pre-commit configuration file includes all relevant files and directories by using wildcards or specific paths as needed.
  7. Failing to test pre-commit hooks locally: Test your hooks locally before pushing changes to ensure they work correctly in your development environment.

Practice Questions

  1. How do you install pre-commit for a Python project?
  2. What is the purpose of the .pre-commit-config.yaml file, and where should it be located in your repository?
  3. How can you add a new check or linter to your pre-commit configuration?
  4. If a commit fails due to pre-commit checks, what steps should you take to address the issues and complete the commit successfully?
  5. What are some common pitfalls to avoid when setting up pre-commit for your project?
  6. How can you ensure that pre-commit runs only on specific files or directories within your repository?
  7. What options does pre-commit offer for handling autofixes and code formatting?
  8. How do you update the pre-commit configuration file to ignore certain files or directories from being checked?
  9. Can you explain how to use wildcards in the .pre-commit-config.yaml file to include multiple files or directories at once?
  10. What are some alternatives to pre-commit for automating code quality checks, and what are their key differences?

FAQ

  1. Can I use pre-commit with other version control systems besides Git?

Pre-commit is designed specifically for Git, but there are similar tools available for other version control systems like SVN and Mercurial.

  1. Can I customize the error messages displayed by pre-commit when a check fails?

Yes, you can create custom error messages by modifying the script or linter that generates the failure message.

  1. Is it possible to run pre-commit only on specific files or directories within my repository?

Yes, you can use regular expressions in your .pre-commit-config.yaml file to specify which files or directories should be affected by each hook.

  1. Can I use pre-commit with multiple programming languages in the same project?

Yes, it is possible to use pre-commit with multiple programming languages by installing and configuring linters for each language separately.

  1. What happens if a commit fails due to pre-commit checks and I force the commit using git commit -f?

Forcing a commit using git commit -f will bypass the pre-commit checks, potentially leading to inconsistencies in your codebase. It is generally best to address any failed checks before committing to maintain a clean and efficient codebase.

  1. What are some alternative tools for automating code quality checks similar to pre-commit?

Some popular alternatives include husky (for Git), eslint-config-prettier, and git-hooks. Researching these tools can help you find the best solution for your specific needs.

Pre-Commit (Git & Dev Tools) | Git & Dev Tools | XQA Learn