git-pre-commit

2019-06-16 admin

git-pre-commit是什么

什么是git-pre-commit,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使用教程帮助文档

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 - <a name=“user-content-number2issue”></a>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!

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-67448.html

文章标题:git-pre-commit

回到顶部