What is simple-git-hooks?
The simple-git-hooks npm package is designed to manage Git hooks in a straightforward and efficient manner. It allows you to easily configure and manage Git hooks using a simple configuration file, making it easier to enforce code quality and automate tasks in your development workflow.
What are simple-git-hooks's main functionalities?
Setup Git Hooks
This feature allows you to set up Git hooks by adding a configuration to your package.json file. The example shows how to run tests before committing and linting before pushing changes.
json
{
"scripts": {
"postinstall": "simple-git-hooks"
},
"simple-git-hooks": {
"pre-commit": "npm test",
"pre-push": "npm run lint"
}
}
Custom Hook Scripts
You can specify custom scripts to be executed for different Git hooks. In this example, custom shell scripts are specified for the pre-commit and pre-push hooks.
json
{
"simple-git-hooks": {
"pre-commit": "./scripts/pre-commit.sh",
"pre-push": "./scripts/pre-push.sh"
}
}
Other packages similar to simple-git-hooks
husky
Husky is a popular package for managing Git hooks. It offers more advanced features and flexibility compared to simple-git-hooks, such as the ability to run hooks conditionally and support for monorepos. However, it can be more complex to set up and configure.
lefthook
Lefthook is another tool for managing Git hooks, focusing on speed and performance. It supports parallel execution of hooks and is designed to work well in large repositories. Lefthook provides a more performant alternative to simple-git-hooks but may require more initial setup.
pre-commit
Pre-commit is a framework for managing and maintaining multi-language pre-commit hooks. It is highly configurable and supports a wide range of hooks out of the box. Pre-commit is more feature-rich and versatile compared to simple-git-hooks, but it can be more complex to configure.
simple-git-hooks
A tool that lets you easily manage git hooks
- Zero dependency
- Small configuration (1 object in package.json)
- Lightweight:
Usage
Add simple-git-hooks to the project
-
Install simple-git-hooks as a dev dependency:
npm install simple-git-hooks --save-dev
-
Add simple-git-hooks
to your package.json
. Fill it with git hooks and the corresponding commands.
For example:
{
"simple-git-hooks": {
"pre-commit": "npx lint-staged",
"pre-push": "cd ../../ && npm run format",
// All unused hooks will be removed automatically by default
// but you can use the `preserveUnused` option like following to prevent this behavior
// if you'd prefer preserve all unused hooks
"preserveUnused": true,
// if you'd prefer preserve specific unused hooks
"preserveUnused": ["commit-msg"]
}
}
This configuration is going to run all linters on every commit
and formatter on push
.
There are more ways to configure the package. Check out Additional configuration options.
-
Run the CLI script to update the git hooks with the commands from the config:
git config core.hooksPath .git/hooks/
rm -rf .git/hooks
npx simple-git-hooks
Now all the git hooks are created.
Update git hooks command
-
Change the configuration.
-
Run npx simple-git-hooks
from the root of your project.
Note for yarn2 users: Please run yarn dlx simple-git-hooks
instead of the command above. More info on dlx
Note that you should manually run npx simple-git-hooks
every time you change a command.
Additional configuration options
You can also add a .simple-git-hooks.cjs
, .simple-git-hooks.js
, simple-git-hooks.cjs
, simple-git-hooks.js
, .simple-git-hooks.json
or simple-git-hooks.json
file to the project and write the configuration inside it.
This way simple-git-hooks
configuration in package.json
will not take effect any more.
.simple-git-hooks.cjs
, .simple-git-hooks.js
or simple-git-hooks.cjs
, simple-git-hooks.js
should look like the following.
module.exports = {
"pre-commit": "npx lint-staged",
"pre-push": "cd ../../ && npm run format",
};
.simple-git-hooks.json
or simple-git-hooks.json
should look like the following.
{
"pre-commit": "npx lint-staged",
"pre-push": "cd ../../ && npm run format"
}
If you need to have multiple configuration files or just your-own configuration file, you install hooks manually from it by npx simple-git-hooks ./my-config.js
.
Uninstall simple-git-hooks
Uninstallation will remove all the existing git hooks.
npm uninstall simple-git-hooks
Common issues
When migrating from husky
git hooks are not running
Why is this happening?
Husky might change the core.gitHooks
value to .husky
, this way, git hooks would search .husky
directory instead of .git/hooks/
.
Read more on git configuration in Git book
You can check it by running this command inside of your repo:
git config core.hooksPath
If it outputs .husky
then this is your case
How to fix?
you need to point core.gitHooks
value to your-awesome-project/.git/hooks
. You can use this command:
git config core.hooksPath .git/hooks/
validate the value is set:
git config core.hooksPath
should output: .git/hooks/
Then remove the .husky
folder that are generated previously by husky
.