Skip to content

Instantly share code, notes, and snippets.

@ozh
Created May 8, 2026 17:06
Show Gist options
  • Select an option

  • Save ozh/d4d7612129add5cd9ec35f257ca53853 to your computer and use it in GitHub Desktop.

Select an option

Save ozh/d4d7612129add5cd9ec35f257ca53853 to your computer and use it in GitHub Desktop.
Run prettier only against files of a PR

This script runs bunx prettier --check against all files committed in a branch that differ from main (git diff main...HEAD). Having --diff-filter=AM means "only Added and Modified" (not deleted)

Workflow:

  • modify, commit
  • check with this script (bash script.sh from repo root)
  • edit last line from --check to --write if wanting to "fix"
  • push
#!/bin/bash

# Get files modified in current branch compared to main
files=$(git diff main...HEAD --name-only --diff-filter=AM)

if [ -z "$files" ]; then
    echo "No modified or added files compared to main."
    exit 0
fi

# Filter only files prettier can handle
prettier_files=$(echo "$files" | grep -E '\.(js|jsx|ts|tsx|md|mdx|json|css|scss|html|yaml|yml)$')

if [ -z "$prettier_files" ]; then
    echo "No files matching prettier extensions."
    exit 0
fi

echo "Running prettier --check on:"
echo "$prettier_files" | sed 's/^/  /'
echo

bunx prettier --check $prettier_files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment