Recently I started to introduce lint-staged
into my Frontend build tool chain. And when I checked the document about it, I always find it works as following:
"husky": {
"hooks": {
"pre-commit": "lint-staged"
},
"lint-staged": {
"src/**/*.{js,jsx,ts,tsx,json,css}": [
"prettier --write",
"eslint --fix src/",
"tslint --fix --project .",
"git add"
]
},
and you can find more similar usage in the link: https://github.com/okonet/lint-staged
My confusing point is the last command git add
, what's the purpose of that?
My understand is lint-staged
only validate the code in staged area after git add
and before git commit
. So can't understand why we need to add one more git add
again.
With lint-staged , executing the command git commit automatically runs the linter against files staged for commit. This lets you continue working on the application code without any interruptions, and once you are done with the code, you can stage the modified files and run the linter before committing them.
git add. The git add command adds a change in the working directory to the staging area. It tells Git that you want to include updates to a particular file in the next commit. However, git add doesn't really affect the repository in any significant way—changes are not actually recorded until you run git commit .
Git Lint is a command line interface for linting Git commits by ensuring you maintain a clean, easy to read, debuggable, and maintainable project history.
You don't need git add
since lint-staged 10
From v10.0.0 onwards any new modifications to originally staged files will be automatically added to the commit. If your task previously contained a
git add
step, please remove this. The automatic behaviour ensures there are less race-conditions, since trying to run multiple git operations at the same time usually results in an error.
Source: https://github.com/okonet/lint-staged#v10
It's using husky to hooks some actions before your commit. See at: https://github.com/typicode/husky
lint-staged
just changes your code and make it linting (It runs before commit by husky). After changed, you need add it again to update git index. And your changes will be effect in your commit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With