Skip to content

Instantly share code, notes, and snippets.

@basperheim
Last active November 4, 2025 13:45
Show Gist options
  • Select an option

  • Save basperheim/a558981175b54c5a351fdcf0352c4c13 to your computer and use it in GitHub Desktop.

Select an option

Save basperheim/a558981175b54c5a351fdcf0352c4c13 to your computer and use it in GitHub Desktop.
Focused Git diffs for humans and LLMs: prints only meaningful changed files (skips assets/vendor dirs), groups by file with headers, and supports path filtering—perfect for pasting compact context into prompts.

git_diff_filtered — focused diffs for humans and LLMs

A small Bash function that prints only the meaningful parts of your Git diff, skipping asset/vendor directories and binary-ish files. Output is grouped by file with a clear header, making it ideal for quickly reviewing changes or pasting compact, high-signal code context into LLM prompts.

Why

  • Less noise, more signal. Excludes node_modules/, .yarn, .astro, and common binary/extensions by default.
  • LLM-friendly. Keeps diffs tight to save tokens and reduce hallucinations; per-file headers help models understand boundaries.
  • Convenient. One call prints all filtered diffs, or you can narrow to a path.

The function

Put this in ~/.bashrc or ~/.zshrc, then source it. (Works best in Bash; in Zsh you can also put it into a #!/usr/bin/env bash script.)

git_diff_filtered() {
cd "$(git rev-parse --show-toplevel)" || return 1
local -a EXCLUDE_DIRS=("node_modules/" ".yarn" ".astro")
local -a EXCLUDE_EXTS=("png" "jpg" "jpeg" "gif" "svg" "ico" "webp" "bmp" "tiff" "mp4" "mp3" "wav" "ogg" "pdf" "woff" "woff2" "ttf" "eot" "zip" "tar" "gz")
local target_file="$1"
# Get all changed files
local changed_files
changed_files=$(git diff --name-only)
if [[ -z "$changed_files" ]]; then
echo "No changes to diff."
return 0
fi
local -a filtered_files=()
while IFS= read -r file; do
local skip=false
# Skip excluded dirs
for pattern in "${EXCLUDE_DIRS[@]}"; do
if [[ "$file" == $pattern* ]]; then
skip=true
break
fi
done
$skip && continue
# Skip excluded extensions
for ext in "${EXCLUDE_EXTS[@]}"; do
if [[ "$file" == *".${ext}" ]]; then
skip=true
break
fi
done
$skip && continue
filtered_files+=("$file")
done <<< "$changed_files"
if [[ ${#filtered_files[@]} -eq 0 ]]; then
echo "No changed files after filtering."
return 0
fi
# Target file filtering
if [[ -n "$target_file" ]]; then
local canonical_arg="${target_file#./}"
local found=0
for file in "${filtered_files[@]}"; do
if [[ "$file" == "$canonical_arg"* ]]; then
echo -e "\n\033[1;36m=== $file ===\033[0m"
git --no-pager diff --color=always -- "$file"
echo
found=1
fi
done
(( found == 0 )) && echo "No changes to diff for '$target_file'."
return 0
fi
# Print all filtered changed files
for file in "${filtered_files[@]}"; do
echo -e "\n\033[1;36m=== $file ===\033[0m"
git --no-pager diff --color=always -- "$file"
echo
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment