Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

git-pre-commit

kazazor1.2kMIT2.1.4

You can run the pre-commit with any build tool (Gulp, Grunt etc..) and it will ignore all the unstaged changes that wasn't added to the git index (using the command git add).

git, pre-commit, precommit, hook, hooks, lint, eslint, jshint, gulp, grunt, npm, yarn, yarn.lock, node, nodejs, js, javascript, ci, development

readme

Build Status Dependency Status devDependency Status

NPM

You can run any pre-commit command (Shell, Gulp, Grunt etc..) and it will ignore all the unstaged changes that wasn't added to the git index (using the command git add).

Installation

First install the package in your devDependencies:

npm install git-pre-commit --save-dev

Or by using yarn:

yarn add git-pre-commit --dev

Using the package

Now, add to your package.json the entry:

"scripts": {
    "precommit": "<task to run>"
}

That is it! No more that you need to do (except for writing what to run :) )

Note: If your root directory has a yarn.lock file, all the scripts will be run using yarn.

Examples

So for example you can do something like that to run Gulp task named pre-commit:

"scripts": {
    "precommit": "gulp lint"
}

Or just a shell command:

"scripts": {
    "precommit": "echo Hello_World"
}

Have fun!

What this package is actually solving?

Most of the git pre-commit hooks are WRONG!
Why? Because most of the pre-commit hooks also take into account the unstaged changes when performing the task.

Lets take for example the most common pre-commit hook: lint.
So what usually people do?
You try to perform a commit and then your pre-commit hook runs and lints all of your files.

There are 2 issues with that common approach:

#1 - When you have some unstaged changes and you would like to commit only the staged changes (the ones you performed git add on) your lint task checks the file itself and doesn't know if the code there will be part of the commit or not.
This resolves into 2 possible situations:

  • The code in the unstaged changes break the lint task - why should I care?!? I'm not trying to commit this code!!
  • The code in the unstaged changes is actualy fixing the lint task, but it is not part of the commit - so for example you got an eslint error on your commit, you fixed it but forgot to git add the changes, now the lint task passes BUT you ended up with the fixing changes outside of your commit.

#2 - It lints all the files and not just the changed files. This is not addressed in this package as it is not the point of it. (For example on how to lint only the changed files you can checkout my eslint example that also uses the git-pre-commit package).

Like I said, this package fixes issue #1 by stashing your unstaged changes and returning the changes to the unstaged state once the pre-commit task has finished (with or without errors).

P.S

Even in this package repository I'm using the package pre-commit hook to lint all of the js files.
For example on how to address the #2 issue:

It lints all the files and not just the changed files

Take a look at my eslint example that does just that. It will save you and your team A LOT of time!

changelog

Change log

NPM

Future release

  • Tests
    • Added Travis-CI build to the repository in order to run the tests on every push / PR. Node.js version to run at build are: 0.12.x, 4.0.x, 4.1.x, 4.2.x, 4.3.x, 5.7.x & 5.x.x - #11.
    • Added tests to the repository using mocha - #12.

v2.1.4

  • Fixes
    • Fixed the usage of the package on Ubuntu - Thanks to @tobiasweibel - #36.

v2.1.3

  • Fixes
    • Fixed the broken module loading on Windows - Thanks to @Goldob - #33.

v2.1.2

  • Improvements
    • Added instructions on how to install the package using yarn.

v2.1.1

  • Fixes
    • Fixed the broken version 2.1.0.

v2.1.0 - Broken version, fixed on version 2.1.1

  • Improvements
    • Added support for yarn. Now if yarn.lock file exists, it will use yarn to execute the Node script instead of npm - #29.

v2.0.1

  • Fixes
    • Fixed the issue for where uninstalled globally executables didn't work for the pre-commit hook - #26.

v2.0.0

  • Breaking changes
    • The package now supports only Node version >=6.0.0
  • Improvements
    • Removed the use of vinyl-fs for faster installation.
    • Changed the codebase to use ES6 syntax.
  • Fixes
    • Removing the use of symlink so installing on Windows machine doesn't require administrator permissions - #19.

v1.0.2

  • Fixes
    • When there is no git repository, exit with no error code (0 instead of 1) - #23
    • Removed fs dependency since it is part of npm core.

v1.0.1

  • Breaking changes
    • Moved the precommit entry to the scripts section in the package.json file.

v0.1.13

  • Fixes
    • Fixed the issue that you couldn't run git commit --amend - #18.

v0.1.11

  • Fixes
    • Fixed the issue where the order of the stashing caused an issue when package.json file has an issue in it - #17.

v0.1.8

  • Fixes
    • Fixed the issue when there isn't any precommit entry in the package.json file and the git commit command actually commits the code - #16.
    • Fixed the issue when you couldn't perform the pre-commit task when the commit was the initial commit in the repository - #8.
  • Development
    • Added dev/dependencies monitoring using https://david-dm.org, to keep the project's dependencies up to date - #15.

v0.1.7 (25-Feb-2016)

  • Fixes
    • Fixed the issue where the pre-commit hook didn't work on SourceTree on OSX machines #2.

v0.1.4 (20-Feb-2016)

  • Fixes
    • Fixed the issue that the package results with an error regarding run-sequence when it was used as a sub module in other projects - #7.

v0.1.1 (19-Feb-2016)

  • Fixes
    • Removed the DEPRECATED gulp-symlink package and use vinyl-fs instead. Now you'll have less warnings in your npm install log.

v0.1.0 (16-Feb-2016)

  • New
    • Released the first working version of the pre-commit hook! Enjoy having you unstaged files ignored :-)