Git hooks are scripts that Git can execute before or after specific events occur, such as a commit, push, or merge. These hooks can be used to perform various tasks, such as running tests, linting code, or enforcing certain guidelines or policies. In this blog we will discuss about pre-commit hooks.
A pre-commit hook is a Git hook that is run before a commit is made. It is used to ensure that the code being committed meets certain quality standards, such as adhering to a specific style guide or passing a certain set of tests.
Pre-commit hooks are useful because they help to catch errors and issues early in the development process, before the code is committed and potentially merged into the main codebase. This can save time and effort by preventing the need to manually fix errors or revert changes after they have been committed.
Common use cases for pre-commit hooks include:
Code formatting: Pre-commit hooks can be used to automatically format the code being committed to adhere to a specific style guide or code formatting standard. This helps to ensure that the code is consistent and readable.
Linting: Pre-commit hooks can be used to run code linters that check for issues such as syntax errors, unused variables, or potential bugs. This can help to catch issues early in the development process and improve the overall quality of the code.
Testing: Pre-commit hooks can be used to run tests on the code being committed to ensure that it passes a certain set of requirements or functionality. This helps to catch bugs and issues early in the development process.
Security checks: Pre-commit hooks can be used to run security checks on the code being committed to ensure that it is free from vulnerabilities or potential security risks.
By using pre-commit hooks, developers can automate many of the repetitive tasks involved in code quality assurance and testing, and ensure that the code being committed meets certain quality standards. This can improve the overall quality of the codebase and make it easier to maintain and collaborate on the project.
In this blog I will share two way by which you can easily initialise pre-commit hook in your git local repo
Method 1. By Editing .git/hooks directory 🔥
The .git/hooks
directory contains a set of sample hook scripts that can be used as a starting point for implementing your own pre-commit hooks. To implement a pre-commit hook using the .git/hooks
file, follow these steps:
Step 1: Navigate to the .git/hooks
directory in your Git repository.
cd .git/hooks
Step 2: Rename the sample pre-commit hook script to a filename that corresponds to your pre-commit hook.
mv pre-commit.sample pre-commit
Step 3: Edit the pre-commit
script using a text editor such as Vim, Nano or Notepad. This script is a simple shell script that is run before a commit is made, and you can add your own commands and logic to this script to perform custom pre-commit checks.
Step 4: Save the changes to the pre-commit
script and exit the text editor.
Here is an example pre-commit hook script that uses gofmt
and goimports
to format your Go code before committing:
#!/bin/bash
# Run gofmt and goimports on all modified .go files
gofmt -w $(git diff --cached --name-only | grep '\.go$')
goimports -w $(git diff --cached --name-only | grep '\.go$')
# Add the modified files back to the staging area
git add $(git diff --cached --name-only)
In this example, we are using the gofmt
and goimports
commands to format the Go code being committed. The git diff
command is used to get a list of all modified .go
files, and then the gofmt
and goimports
commands are run on those files. Finally, the modified files are added back to the staging area.
Once you have created your pre-commit hook script, you can test it by making changes to your Git repository and then running git commit
. If the pre-commit hook fails, the commit will be aborted, and you will need to fix the issues before you can commit your changes.
Method 2. By adding .pre-commit-config.yaml
file in your repository root directory. (e.g. repo) 🔥
The .pre-commit-config.yaml
file is a configuration file that specifies which Git hooks to use and how to use them.
To implement pre-commit Git hooks using .pre-commit-config.yaml
, you will first need to install the pre-commit tool. You can do this by running the following command:
using pip:
pip install pre-commit
using homebrew:
brew install pre-commit
Once you have installed the pre-commit tool, you can create a .pre-commit-config.yaml
file in the root directory of your Git repository. This file will specify the Git hooks that you want to use and how to use them.
Here is an example .pre-commit-config.yaml
file that runs the gofmt
and goimports
Git hooks to format your Go code before committing:
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
hooks:
- id: gofmt
- id: goimports
In this example, we are using the pre-commit-hooks repository to get the gofmt
and goimports
hooks. The rev
parameter specifies which version of the repository to use.
After creating the .pre-commit-config.yaml
file, you can run the following command to install the Git hooks:
$ pre-commit install
pre-commit installed at .git/hooks/pre-commit
This command will install the Git hooks specified in the .pre-commit-config.yaml
file, and they will now be run automatically every time you run git commit
.
If the Git hooks fail, the commit will be aborted, and you will need to fix the issues before you can commit your changes. This helps to ensure that your code meets certain quality standards before it is committed, making it easier to maintain and collaborate on your project.
In summary, pre-commit hooks are an important tool for ensuring code quality and catching issues early in the development process. By using the
.git/hooks
file to implement custom pre-commit hooks, developers can automate repetitive tasks such as code formatting, linting, testing, and security checks, and improve the overall quality of the codebase.While the
.git/hooks
file is a simple and easy way to implement pre-commit hooks, it can become cumbersome to manage as the number of hooks grows. An alternative approach is to use a pre-commit tool and a.pre-commit-config.yaml
file, which provides a more scalable and maintainable way to manage pre-commit hooks.Regardless of the approach used, it’s important to choose pre-commit hooks that are relevant to your project and help to maintain a high standard of code quality. By using pre-commit hooks, developers can save time and effort, improve collaboration, and build better software.
That’s all for now in the next blog I will explain the advance method to implement the git hooks for your repository or organisation.
Thank you for your interest in this article, if you like the content feel free to follow, clap and share it ❤️🙏