In this lesson, you will learn:
- what pre-commit is
- how to trigger our code quality tools at the point of a git commit
- how to install and configure pre-commit.
Rather than having to remember to manually run our code quality tools, we can use a tool called pre-commit.
What is pre-commit?
pre-commit is a tool that allows you to trigger different tasks and actions against your code at the point when you perform a git commit. The key benefit of pre-commit is that it prevents you from forgetting to run the required actions against your code. Not only this, but it allows you to share your pre-commit configuration to ensure the same tools and actions are being performed against your codebase.
How Does pre-commit Work?
pre-commit works by leveraging git hooks. Git hooks provide us with the ability to run a series of different shell commands at different trigger points within Git (such as push, merge, commit). These hooks are configured via files within the
.git/hooks directory (shown below):
.git/hooks ├── applypatch-msg.sample ├── commit-msg.sample ├── fsmonitor-watchman.sample ├── post-update.sample ├── pre-applypatch.sample ├── pre-commit.sample ├── pre-merge-commit.sample ├── pre-push.sample ├── pre-rebase.sample ├── pre-receive.sample ├── prepare-commit-msg.sample └── update.sample