The idea of git hooks is for preventing the wrong source code from being committed. I use husky
for this purpose. Here is my setting:
"husky": {
"hooks": {
"pre-commit": "ng lint --fix=true"
}
}
If there is a lint error, it prevents from being committed. Here is the scenario:
- Developer writes wrong code (e.g.
x==y
which should bex===y
from lint point of view) - He uses these commands:
git add .
andgit commit -m "msg here"
which fails. Because lint fails. - He corrects the source code. But instead of doing
git add .
again, he uses this command:git commit -m "msg here"
. This time lint doesn't fail, because in source code everything is fine, andng lint
succeeds. But actually, he is committing the previous version which has been added before.
Any idea how to prevent this?
lint-staged
is what you are looking for.It will only lint the staged files, which is really efficient.
That means that it would force the developer in your example to stage the fix before
lint-staged
will accept it.Put this in your package.json file: