# simple-git-hooks ![](https://img.shields.io/badge/dependencies-zero-green) [![Tests](https://github.com/toplenboren/simple-git-hooks/actions/workflows/tests.yml/badge.svg?branch=master)](https://github.com/toplenboren/simple-git-hooks/actions/workflows/tests.yml) A tool that lets you easily manage git hooks - Zero dependency - Small configuration (1 object in package.json) - Lightweight: | Package | Unpacked size | With deps | |---------|-------------|-----------| | husky `9.1.7` | `4.0 kB` | `4.0 kB` | | **simple-git-hooks** `2.13.1` | `13.0 kB` | `13.0 kB` | | lefthook `2.1.10` | `28.4 kB` | `28.4 kB` | | pre-commit `2.0.0` | `35.9 kB` | `~850 kB` | ## Who uses simple-git-hooks? - [PostCSS](https://github.com/postcss/postcss) - [Nano ID](https://github.com/ai/nanoid) - [VitePress](https://github.com/vuejs/vitepress) - [VueUse](https://github.com/vueuse/vueuse) - [Slidev](https://github.com/slidevjs/slidev) - [Vercel/pkg](https://github.com/vercel/pkg) - [WXT](https://github.com/wxt-dev/wxt) - [Elk](https://github.com/elk-zone/elk) - More, see [full list](https://github.com/toplenboren/simple-git-hooks/network/dependents?package_id=UGFja2FnZS0xOTk1ODMzMTA4) ### What is a git hook? A git hook is a command or script that is going to be run every time you perform a git action, like `git commit` or `git push`. If the execution of a git hook fails, then the git action aborts. For example, if you want to run `linter` on every commit to ensure code quality in your project, then you can create a `pre-commit` hook that would call `npx lint-staged`. Check out [lint-staged](https://github.com/okonet/lint-staged#readme). It works really well with `simple-git-hooks`. You can look up about git hooks on the [Pro Git book](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks). ### When to use it `simple-git-hooks` works well for small-sized projects when you need quickly set up hooks and forget about it. However, this package requires you to manually apply the changes to git hooks. If you update them often, this is probably not the best choice. Also, this package allows you to set only one command per git hook. If you need multiple verbose commands per git hook, flexible configuration or automatic update of git hooks, please check out the other packages: - [Lefthook](https://github.com/Arkweid/lefthook) - [husky](https://typicode.github.io/husky/) - [pre-commit](https://github.com/pre-commit/pre-commit) ## Usage ### Add simple-git-hooks to the project 1. Install simple-git-hooks as a dev dependency: ```sh npm install simple-git-hooks --save-dev ``` 2. Add `simple-git-hooks` to your `package.json`. Fill it with git hooks and the corresponding commands. For example: ```jsonc { "simple-git-hooks": { "pre-commit": "npx lint-staged", "pre-push": "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](#additional-configuration-options). 3. Run the CLI script to update the git hooks with the commands from the config: ```sh npx simple-git-hooks ``` Now all the git hooks are created. 4. Add simple-git-hooks to `prepare` for automatic hooks installation ```jsonc { "scripts": { "prepare": "existing-command && simple-git-hooks" } } ``` Now git hooks will be installed automatically when `npm install` is run ### Update git hooks command 1. Change the configuration. 2. 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](https://yarnpkg.com/cli/dlx) Note for **yarn1** users: Please run `ynpx simple-git-hooks` instead of the command above. More info on [ynpx](https://npm.io/package/ynpx) 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.mjs`, `simple-git-hooks.cjs`, `simple-git-hooks.js`, `simple-git-hooks.mjs`, `.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`, `.simple-git-hooks.mjs` or `simple-git-hooks.cjs`, `simple-git-hooks.js`, `simple-git-hooks.mjs` should look like the following. #### CommonJS ```js module.exports = { "pre-commit": "npx lint-staged", "pre-push": "npm run format", }; ``` #### ES Modules ```js export default { "pre-commit": "npx lint-staged", "pre-push": "npm run format", }; ``` `.simple-git-hooks.json` or `simple-git-hooks.json` should look like the following. ```json { "pre-commit": "npx lint-staged", "pre-push": "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`. ### Note for `npm` package developers Please do not add `postinstall: "npx simple-git-hooks"` script in your `package.json`. Or at least remove it before `npm publish` It causes errors for end users of your package ### Uninstall simple-git-hooks > Uninstallation will remove all the existing git hooks. ```sh pnpm uninstall simple-git-hooks ``` Note: It only works for some 3rd-party package managers e.g. pnpm. For **npm** - default node package manager - it isn't supported, see [documentation](https://docs.npmjs.com/cli/v11/using-npm/scripts#a-note-on-a-lack-of-npm-uninstall-scripts). You can uninstall hooks manually from your project root: ```sh node node_modules/simple-git-hooks/uninstall.js ``` ## Common issues ### Scripts are not installed automatically / Postinstall (npm [RFC #868](https://github.com/npm/rfcs/pull/868)) `simple-git-hooks` runs a `postinstall` script when installed as a dependency, so hooks appear on their own after `npm install`. Modern package managers block dependency install scripts by default for security reasons: - pnpm, Yarn Berry, and Bun already block them today. - npm v12+ will also block them via [RFC #868](https://github.com/npm/rfcs/pull/868). If your package manager blocks the built-in `postinstall`, you have two options: #### Best solution: Add `simple-git-hooks` to `prepare` of package.json of your project This works with all package managers (npm, pnpm, Yarn, Bun) and requires no allowlist. ```jsonc { "scripts": { "prepare": "existing-command && simple-git-hooks" } } ``` #### Workaround: Allow the built-in `postinstall` to run If you prefer to keep using the built-in postinstall (the default behavior where supported), you must explicitly allow it. > Note: Postinstall script is planned to be removed in `3.x.x` **npm (RFC #868)** ```jsonc { "allowScripts": ["simple-git-hooks"] } ``` Then run `npm approve-scripts simple-git-hooks`. **pnpm** ```yaml # pnpm-workspace.yaml allowBuilds: simple-git-hooks: true ``` Or run `pnpm approve-builds simple-git-hooks`. **Yarn Berry** ```jsonc { "dependenciesMeta": { "simple-git-hooks": { "built": true } } } ``` **bun** Bun does not support dependency install scripts. Use the prepare solution above. ### I want to skip git hooks! If you need to bypass install hooks at all, for example on CI, you can use `SKIP_INSTALL_SIMPLE_GIT_HOOKS` environment variable at the first place. ```sh export SKIP_INSTALL_SIMPLE_GIT_HOOKS=1 npm install simple-git-hooks --save-dev ``` Or if you only need to bypass hooks for a single git operation, you should use `--no-verify` option ```sh git commit -m "commit message" --no-verify # -n for shorthand ``` you can read more about it here https://bobbyhadz.com/blog/git-commit-skip-hooks#skip-git-commit-hooks If you need to bypass hooks for multiple Git operations, setting the SKIP_SIMPLE_GIT_HOOKS environment variable can be more convenient. Once set, all subsequent Git operations in the same terminal session will bypass the associated hooks. ```sh # Set the environment variable export SKIP_SIMPLE_GIT_HOOKS=1 # Subsequent Git commands will skip the hooks git add . git commit -m "commit message" # pre-commit hooks are bypassed git push origin main # pre-push hooks are bypassed ``` ### Skipping Hooks in 3rd party git clients If your client provides a toggle to skip Git hooks, you can utilize it to bypass the hooks. For instance, in VSCode, you can toggle git.allowNoVerifyCommit in the settings. If you have the option to set arguments or environment variables, you can use the --no-verify option or the SKIP_SIMPLE_GIT_HOOKS environment variable. If these options are not available, you may need to resort to using the terminal for skipping hooks. ### I am getting "npx: command not found" error in a GUI git client This happens when using a node version manager such as `nodenv`, `nvm`, `mise` which require init script to provide project-specific node binaries. Create init script in `~/.simple-git-hooks.rc` that should be executed prior to git hooks. Please refer to your node manager documentation for details. For example, for mise, that will be: ```sh export PATH="$HOME/.local/share/mise/shims:$PATH" ``` Add `SIMPLE_GIT_HOOKS_RC` global environment variable pointing to that new script. For example, on macOS, add this to `~/.zshenv`: ```sh export SIMPLE_GIT_HOOKS_RC="$HOME/.simple-git-hooks.rc" ``` ### 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](https://git-scm.com/docs/githooks) 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`.