Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prettier using pre-commit(.com) does not re-stage changes

I started using Prettier + eslint using pretty-quick & husky (option 2). It behaves as-expected, i.e., reformats code during a commit (or amend) and includes the changes in the commit.

My monorepo needed (several) more pre-commit hooks, so I ended up migrating to pre-commit.com (option 3). Now, when I commit or amend, Prettier modifies the files and returns Failed status. This creates for a rather annoying workflow where I am forced to add the files and attempt to commit the changes again.

Is there some way to re-stage the changes as part of the commit?

like image 873
Zane Claes Avatar asked Jul 12 '26 00:07

Zane Claes


2 Answers

pre-commit intentionally never touches the staging area. silently changing what is being committed is dangerous. the tool always gives you an opportunity to validate what the hooks do before committing

you can read more about this in the issue tracker:

  • https://github.com/pre-commit/pre-commit/issues/532
  • https://github.com/pre-commit/pre-commit/issues/747
  • https://github.com/pre-commit/pre-commit/issues/806
  • https://github.com/pre-commit/pre-commit/issues/879
  • ... and many more

My recommendation if you want to fire from the hip is to run git add -u && !! immediately afterwards -- this will stage the changed files and rerun the command you just ran

if you want to venture into unsupported territory, you can run this as part of the command (as demonstrated in one of the issues) -- but this subtly breaks a lot of the guarantees that the tool has

  - id: yapf
    entry: bash -c 'yapf "$@"; git add -u' --

disclaimer: I am the author of pre-commit

like image 123
Anthony Sottile Avatar answered Jul 14 '26 22:07

Anthony Sottile


I'm sure my older brother, the creator of Pre-commit will confirm this, but this is the intended behavior of pre-commit. It should fail because it is directly modifying your files. I use this exact setup using Prettier for myself actually, and while it is annoying, it's just one of those things you gotta change. I like making a git commit -m called 'format' or something and then a commit for the actual change.

edit: he sniped me before I could answer, his answer goes much more in depth

like image 35
Daniel Sottile Avatar answered Jul 14 '26 23:07

Daniel Sottile



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!