Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A simple git hooks manager for small projects

License

NotificationsYou must be signed in to change notification settings

toplenboren/simple-git-hooks

Repository files navigation

Tests

A tool that lets you easily manage git hooks

The package was recently renamed fromsimple-pre-commit.

SeeReleases for thesimple-pre-commit documentation and changelog

  • Zero dependency

  • Small configuration (1 object in package.json)

  • Lightweight:

    PackageUnpacked sizeWith deps
    husky v44.3.853.5 kB~1 mB
    husky v88.0.36.44 kB6.44 kB
    pre-commit1.2.2~80 kB~850 kB
    simple-git-hooks2.11.010.9 kB10.9 kB

Who uses simple-git-hooks?

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, likegit commit orgit push.

If the execution of a git hook fails, then the git action aborts.

For example, if you want to runlinter on every commit to ensure code quality in your project, then you can create apre-commit hook that would callnpx lint-staged.

Check outlint-staged. It works really well withsimple-git-hooks.

You can look up about git hooks on thePro Git book.

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:

Usage

Add simple-git-hooks to the project

  1. Install simple-git-hooks as a dev dependency:

    npm install simple-git-hooks --save-dev
  2. Addsimple-git-hooks to yourpackage.json. Fill it with git hooks and the corresponding commands.

    For example:

    {"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 everycommit and formatter onpush.

    There are more ways to configure the package. Check outAdditional configuration options.

  3. Run the CLI script to update the git hooks with the commands from the config:

    npx simple-git-hooks

Now all the git hooks are created.

Update git hooks command

  1. Change the configuration.

  2. Runnpx simple-git-hooksfrom the root of your project.

Note foryarn2 users: Please runyarn dlx simple-git-hooks instead of the command above. More info ondlx

Note foryarn1 users: Please runynpx simple-git-hooks instead of the command above. More info onynpx

Note that you should manually runnpx simple-git-hooksevery 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 orsimple-git-hooks.json file to the project and write the configuration inside it.

This waysimple-git-hooks configuration inpackage.json will not take effect any more.

.simple-git-hooks.cjs,.simple-git-hooks.js,.simple-git-hooks.mjs orsimple-git-hooks.cjs,simple-git-hooks.js,simple-git-hooks.mjs should look like the following.

CommonJS

module.exports={"pre-commit":"npx lint-staged","pre-push":"npm run format",};

ES Modules

exportdefault{"pre-commit":"npx lint-staged","pre-push":"npm run format",};

.simple-git-hooks.json orsimple-git-hooks.json should look like the following.

{"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 bynpx simple-git-hooks ./my-config.js.

Note fornpm package developers

Please do not addpostinstall: "npx simple-git-hooks" script in yourpackage.json. Or at least remove it beforenpm publish

It causes errors for end users of your package

Uninstall simple-git-hooks

Uninstallation will remove all the existing git hooks.

npm uninstall simple-git-hooks

Common issues

I want to skip git hooks!

If you need to bypass install hooks at all, for example on CI, you can useSKIP_INSTALL_SIMPLE_GIT_HOOKS environment variable at the first place.

export SKIP_INSTALL_SIMPLE_GIT_HOOKS=1npm 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

git commit -m"commit message" --no-verify# -n for shorthand

you can read more about it herehttps://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.

# Set the environment variableexport SKIP_SIMPLE_GIT_HOOKS=1# Subsequent Git commands will skip the hooksgit add.git commit -m"commit message"# pre-commit hooks are bypassedgit 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 asnodenv,nvm,mise which requireinit 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 willbe:

export PATH="$HOME/.local/share/mise/shims:$PATH"

AddSIMPLE_GIT_HOOKS_RC global environment variable pointing to that new script. Forexample, on macOS, add this to~/.zshenv:

export SIMPLE_GIT_HOOKS_RC="$HOME/.simple-git-hooks.rc"

When migrating fromhusky git hooks are not running

Why is this happening?

Husky might change thecore.gitHooks value to.husky, this way, git hooks would search.husky directory instead of.git/hooks/.

Read more on git configuration inGit 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 pointcore.gitHooks value toyour-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 byhusky.

About

A simple git hooks manager for small projects

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors28


[8]ページ先頭

©2009-2025 Movatter.jp