Code Monkey home page Code Monkey logo

simple-git-hooks's Introduction

simple-git-hooks

Tests

A tool that lets you easily manage git hooks

The package was recently renamed from simple-pre-commit.

See Releases for the simple-pre-commit documentation and changelog

  • Zero dependency

  • Small configuration (1 object in package.json)

  • Lightweight:

    Package Unpacked size With deps
    husky v4 4.3.8 53.5 kB ~1 mB
    husky v6 6.0.0 6.86 kB 6.86 kB
    pre-commit 1.2.2 ~80 kB ~850 kB
    simple-git-hooks 2.2.0 10.1 kB 10.1 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, 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. It works really well with simple-git-hooks.

You can look up about git hooks on the Pro 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. 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.

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

    # [Optional] These 2 steps can be skipped for non-husky users
    git config core.hooksPath .git/hooks/
    rm -rf .git/hooks
    
    # Update ./git/hooks
    npx simple-git-hooks

Now all the git hooks are created.

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

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

I want to skip git hooks!

You should use --no-verify option

https://bobbyhadz.com/blog/git-commit-skip-hooks#skip-git-commit-hooks

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.

simple-git-hooks's People

Contributors

toplenboren avatar jounqin avatar ai avatar djaler avatar tomeraberbach avatar valtlai avatar ddialar avatar flugg avatar 3c1u avatar jesse-savary avatar silvenon avatar pustovalov avatar motss avatar samydoesit avatar bhanufyi avatar losadaem avatar yokinist avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.