I have an Angular app using git and Visual Studio Code. I set up ESLint and am using husky to run lint-staged
in a pre-commit hook, so that changes with linting errors cannot be committed.
When I first set it up, the pending changes in failed commits would just disappear, and I learned that they were getting stashed: https://mcmap.net/q/570988/-why-are-my-changes-gone-after-a-cancelled-git-commit-and-how-do-i-recover-them.
I don't understand why it would default to stashing my changes away, rather than letting me just correct the linting error and try the commit again. It seems bizarre, quite frankly. It's going to panic every developer who ever runs into it. So I added the --no-stash
flag so it wouldn't do that.
/.husky/pre-commit:
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npx lint-staged --no-stash
But now I stumbled on another problem case. Steps:
- Make a change in a file and accidentally leave in a linting error.
- Stage this file.
- Make another change in the same file (but don't stage it).
- Try to commit the staged changes. It will fail due to the linting error, and the second change will be lost.
If you happen to have the file open, you may be able to recover your changes via 'Undo' in the IDE, but this is a big problem. How can I prevent ever losing work due to rejected commits, while also not having work automatically stashed?