14

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.

1
  • 2
    --fix probably rewrites the files according to some sort of rules, so now the re-written files in the work-tree need to be copied over the earlier, not-rewritten files that you had copied into the staging area earlier.
    – torek
    Commented Jan 28, 2019 at 7:21

2 Answers 2

32

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

1
5

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.

2

Not the answer you're looking for? Browse other questions tagged or ask your own question.