Skip to content

Git patch-id Cheat Sheet ​

bash
# --- Basic Usage ---

# Get patch-id for current commit
git show HEAD | git patch-id --stable

# Get patch-id for a specific commit
git show <SHA> | git patch-id --stable

# Extract just the patch-id (first field)
git show HEAD | git patch-id | cut -d' ' -f1

# List all commits in a range with their patch-ids
git log --patch --format="commit %H" <range> | git patch-id --stable

# --- Cherry (patch-id wrapper) ---

# Find commits in HEAD not yet applied to main
git cherry -v main HEAD
#   + = not applied   - = already patch-equivalent on main

# Backport coverage report
git cherry -v origin/release/v2.0 origin/release/v2.1

# --- DevOps One-Liners ---

# Backport fidelity check
[ "$(git show <ORIG> | git patch-id | cut -d' ' -f1)" = "$(git show <BACKPORT> | git patch-id | cut -d' ' -f1)" ] && echo "MATCH"

# Pre-merge duplicate gate
for pid in $(git log --patch --format="commit %H" main..HEAD | git patch-id | cut -d' ' -f1); do
  git log --patch --format="commit %H" main~200..main | git patch-id | grep -q "^$pid" && echo "DUPLICATE: $pid" && exit 1
done

# Post-rebase integrity
git log --patch --format="commit %H" <base>..ORIG_HEAD | git patch-id | sort > /tmp/pre
git log --patch --format="commit %H" <base>..HEAD | git patch-id | sort > /tmp/post
diff /tmp/pre /tmp/post

# CVE consistency across branches
for b in v2.1 v2.0 v1.9; do
  git log --patch --format="commit %H" "$b" | git patch-id | grep "^$(git show <CVE_SHA> | git patch-id | cut -d' ' -f1)" && echo "$b: OK" || echo "$b: MISSING"
done

# Count patch-equivalent commits between branches
git cherry main HEAD | grep '^-' | wc -l

# --- Config ---

# Git 2.37+ uses --stable by default; always be explicit
git config --global alias.patch-id '!f() { git show "$1" | git patch-id --stable | cut -d" " -f1; }; f'

Key flags ​

FlagEffect
--stable(default Git 2.37+) Stable mode — includes blob SHAs from index lines
--unstableLegacy mode — excludes index lines (pre-2.37 behavior)
--verbatim(Git 2.42+) Include all diff headers in computation

Exit codes ​

CodeMeaning
0Successfully computed patch-id(s)
1Error (no valid diff on stdin, or unknown flag)

See also ​

  • docs/devops-workflows/git-patch-id.md — full guide with 6 DevOps patterns
  • docs/devops-workflows/git-cherry-pick.md — selective commit backport
  • docs/devops-workflows/git-range-diff.md — compare commit ranges
  • scripts/patch-id-manage.sh — automation utility

Git Master — Git Reference for Every Skill Level