What is pre-commit?
Pre-commit is a tool that checks your code before you commit it to your repository. Think of it as a friendly assistant that reviews your code and helps catch common mistakes before they become part of your project. Why should I use it?- Catches mistakes early (before they get into your codebase)
- Keeps code style consistent across your team
- Only checks files you’ve changed, saving time
- Works with many programming languages
- Easy to set up and share with your team
- Stops commits if it finds problems, helping maintain quality
Key concept
Checks (Hooks)Pre-commit runs checks on your code before each commit. Common checks include:
- Fixing code formatting
- Finding potential errors
- Checking for security issues
- Making sure files aren’t too large
- Looking for common mistakes
Setup
All the checks are listed in a simple configuration file.pre-commit-config.yaml at the root of your project . You can choose which checks you want to run and how they should work.
Running
Once set up, pre-commit automatically runs whenever you try to commit code. It will:- Look at the files you’ve changed
- Run your chosen checks
- Either allow the commit (if everything passes) or show you what needs to be fixed