I use a pre-commit hook to run Prettier formatter against my HTML documents:
#!/bin/sh
# Retrieving a list of the staged files
stagedFiles=$(git diff --staged --name-only)
# Format staged files automatically
echo "Formatting with Prettier..."
npx prettier --write $stagedFiles
git add $stagedFiles
The formatting successfully applies right after I enter git commit command and right before my configured editor opens the commit message. According to commit.verbose setting, Git appends there a diff showing what is to be committed, but this diff does not take into account changes made by Prettier — it demonstrates the unformatted version of my code. If I then abort the commit (leaving the message empty), I can see that no unstaged changes are in the repository; it means that all formatting modifications had been properly staged during execution of the pre-commit hook. Moreover, if I write some text to the message and complete the commit without aborting, the resulting snapshot does contain all formatting that was missing in the diff.
I finally concluded that the diff lies. But does a solution exist?
Git for Windows version: 2.33.0
OS: Windows 10 21h1 x64