Skip to content

Instantly share code, notes, and snippets.

@peshala-prabhapoorna
Last active June 28, 2025 14:15
Show Gist options
  • Select an option

  • Save peshala-prabhapoorna/0537c685c1b54ed36ed07f21d32e27a6 to your computer and use it in GitHub Desktop.

Select an option

Save peshala-prabhapoorna/0537c685c1b54ed36ed07f21d32e27a6 to your computer and use it in GitHub Desktop.
Prettier pre-commit hook

Prettier Pre-commit Hook

Re-format your files that are marked as “staged” with Prettier. Save this script as .git/hooks/pre-commit and give it execute permission.

Pre-commit Hook · Prettier.

#!/bin/sh
# Re-format your files that are marked as “staged” with Prettier
FILES=$(git diff --cached --name-only --diff-filter=ACMR | sed 's| |\\ |g')
[ -z "$FILES" ] && exit 0
# Prettify all selected files
echo "$FILES" | xargs ./node_modules/.bin/prettier --ignore-unknown --write
# Add back the modified/prettified files to staging
echo "$FILES" | xargs git add
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment