Skip to content

pathspec quick ​

Magic signatures ​

| :(top) | Path from repo root, ignores cwd | | :(literal) | Disable glob/wildcard interpretation | | :(icase) | Case-insensitive match | | :(glob) | Full globs: **, [a-z], {a,b} | | :(attr:key=val) | Match by .gitattributes attribute | | :(exclude) | Invert match — exclude paths |

Short forms: :/ = top, :! = exclude, :\ = literal.

Combine: :(top,glob,exclude)**/test/**

CI patterns ​

bash
# Exclude lockfiles from change detection
git diff --name-only HEAD~1 -- . ':!package-lock.json'

# Root-relative diff from any working dir
git diff --name-only HEAD~1 -- :(top)src/

# Only infrastructure files (define in .gitattributes)
git diff --exit-code HEAD~1 -- :(attr:infrastructure=true) || echo "infra changed"

# Global search exclude
git grep -l 'TODO' -- :(exclude,glob)**/generated/**

# Docs-only gate
git diff --exit-code HEAD~1 -- . ':(exclude,glob)**/*.md' || echo "docs-only=true"

Pitfalls ​

  • Shell quoting: always quote pathspecs (':(exclude)*.lock')
  • Exclude order: include first, exclude after (-- . ':(exclude)foo')
  • Exclude alone matches nothing: pair with . or a directory
  • attr needs the attribute defined: files without it are not matched
  • glob * still doesn't cross /: use **/ for deep matching

Git Master — Git Reference for Every Skill Level