DEV Community

angu10
angu10

Posted on • Updated on

Setting Up Pre-Commit Hooks in GitHub: Ensuring Code Quality and Consistency

Introduction:
Pre-commit hooks are a powerful tool that can help maintain code quality, enforce style guidelines, and prevent common mistakes in software development. In this blog post, we will explore how to set up pre-commit hooks for your entire team using GitHub. Specifically, we will discuss the process of setting up pre-commit hooks for popular tools such as Black, pre-commit-hooks, Prettier, and pylint.

Table of Contents:

1.What are Pre-Commit Hooks?
2.Benefits of Pre-Commit Hooks
3.Setting Up Pre-Commit Hooks in GitHub
a.Prerequisites
b.Configuring the pre-commit Configuration File
c.Installing and Initializing Pre-Commit
d.Adding Pre-Commit Hooks

  1. Commonly Used Pre-Commit Hooks a.Black b.pre-commit-hooks c.Prettier d.pylint
  2. Customizing Pre-Commit Hooks
  3. Running Pre-Commit Hooks
  4. Conclusion

Sure! Here are the installation steps and code snippets for each section in the table of contents:

1. What are Pre-Commit Hooks?

Pre-commit hooks are scripts or actions that are automatically executed before a commit is made to a version control system. They help enforce code quality standards and catch potential issues before they are committed.

2. Benefits of Pre-Commit Hooks

Using pre-commit hooks in your development workflow offers several benefits:

  • Ensuring code quality and consistency
  • Enforcing style guidelines and formatting standards
  • Preventing common mistakes or issues
  • Catching potential bugs or vulnerabilities early
  • Facilitating collaboration and reducing code review efforts

3. Setting Up Pre-Commit Hooks in GitHub

a. Prerequisites

  • Git installed on your system
  • A project directory set up with a Git repository

b. Configuring the pre-commit Configuration File

Create a file called .pre-commit-config.yaml in the root of your project directory. This file will contain the configuration for your Pre-Commit hooks.

c. Installing and Initializing Pre-Commit

pip install pre-commit
pre-commit init
Enter fullscreen mode Exit fullscreen mode

d. Adding Pre-Commit Hooks

In the .pre-commit-config.yaml file, define the hooks you want to use. For example, to use the Black code formatter:

repos:
  - repo: https://github.com/psf/black
    rev: <version>
    hooks:
      - id: black
Enter fullscreen mode Exit fullscreen mode

Replace <version> with the desired version of Black.

4. Commonly Used Pre-Commit Hooks

a. Black

Installation:

pip install black
Enter fullscreen mode Exit fullscreen mode

Configuration in .pre-commit-config.yaml:

repos:
  - repo: https://github.com/psf/black
    rev: <version>
    hooks:
      - id: black
Enter fullscreen mode Exit fullscreen mode

b. pre-commit-hooks

Installation:

pip install pre-commit-hooks
Enter fullscreen mode Exit fullscreen mode

Configuration in .pre-commit-config.yaml:

repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: <version>
    hooks:
      - id: check-json
Enter fullscreen mode Exit fullscreen mode

c. Prettier

Installation:

npm install --save-dev prettier
Enter fullscreen mode Exit fullscreen mode

Configuration in .pre-commit-config.yaml:

repos:
  - repo: https://github.com/pre-commit/mirrors-prettier
    rev: <version>
    hooks:
      - id: prettier
    files: \.(json|markdown|md|yaml|yml)$
Enter fullscreen mode Exit fullscreen mode

d. pylint

Installation:

pip install pylint
Enter fullscreen mode Exit fullscreen mode

Configuration in .pre-commit-config.yaml:

repos:
  - repo: https://github.com/PyCQA/pylint
    rev: <version>
    hooks:
      - id: pylint
        name: pylint
        entry: pylint
        language: system
        types: [python]
        env:
          PYTHONPATH: "./"
Enter fullscreen mode Exit fullscreen mode

5. Customizing Pre-Commit Hooks

You can customize pre-commit hooks by modifying the .pre-commit-config.yaml file. This includes specifying hook options, excluding files or directories, or defining additional hooks or scripts.

6. Running Pre-Commit Hooks

To run pre-commit hooks before a commit, simply make a commit using Git. The hooks will automatically be executed. To manually run the hooks without making a commit, use the command pre-commit run --all-files.

7. Conclusion

In this blog post, we have explored how to set up and use Pre-Commit hooks in GitHub. By following these steps and configuring the hooks, you can ensure code quality, enforce style guidelines, and catch potential issues early in your development workflow. Pre-Commit hooks offer numerous benefits and can greatly improve collaboration and code consistency within your team.

Top comments (0)