Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lint-staged: what the purpose of 'git add' command

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.

like image 438
Chris Bao Avatar asked Jan 28 '19 06:01

Chris Bao


People also ask

What is git add in lint-staged?

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.

What is the purpose of git add?

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 .

What is lint in git?

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.


Video Answer


2 Answers

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

like image 86
Do Async Avatar answered Oct 18 '22 07:10

Do Async


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.

like image 22
Chien Nguyen Avatar answered Oct 18 '22 08:10

Chien Nguyen