4

TL;DR: Using eslint --fix && git add in a pre-commit hook works but it leaves staged and not staged files! Using a post-commit hook to git reset <staged-files> seems to work but is there a solution for this?

To give some background, I started using lint-staged and husky to automatically fix JavaScript lint errors on git commit. This works extremely well if you staged your changes first and then commit, for example:

git add index.js
git commit -m "Some Message"

IntelliJ IDEA seems to commit files directly (e.g. git commit -m "Some Message" index.js) which still works, the automatically fixed files are committed. Unfortunately, it also leaves a staged file (auto-fixed) and a not staged file (original lint errors) and this is the problem I am wanting to resolve.

Not knowing where to begin I opened issue 151 with lint-staged but after some investigation, the problem seems to be related to Git and not lint-staged.

I only have basic Git skills, so to minimize distractions, I created a project without lint-staged or husky and manually added a pre-commit hook. I then ran the same tests (e.g. git commit -m "Some Message" index.js) and unsurprisingly the same issue appeared. In an attempt to resolve the problem I also added a post-commit hook that runs git reset <staged-files> which appears to work well, but I'm unsure if it's the correct solution.

If you have experience with this or you're a Git Guru, take a look at the demo repo and let us know if there is a better way. Any help will be greatly appreciated.

Samuel Sharpe
  • 196
  • 1
  • 6

1 Answers1

1

See https://youtrack.jetbrains.com/oauth?state=%2Fissue%2FIDEA-135454

Workaround: Add git update-index -g as postcommit hook.

Jonas Gröger
  • 1,558
  • 2
  • 21
  • 35