Git Reference ยท Plumbing Commands
git patch-id command
Reference for git patch-id: compute a hash of a patch that ignores line numbers and whitespace, with --stable, --unstable and --verbatim, plus cherry detection.
git patch-id reads a patch (the output of git diff, git show or git log -p) and prints a hash of the change itself. Two patches that make the same edit get the same ID, even when they sit at different line numbers, in different commits, or on different branches. Commit hashes cannot tell you that, because a cherry-picked or rebased commit gets a new hash.
This is how git cherry, git rebase and git log --cherry-mark decide that a commit has "already been applied" upstream. You can use it yourself to compare patches, de-duplicate a mailing-list series or find a backported fix.
git show <commit> | git patch-id git log -p main..topic | git patch-id
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
git patch-id [--stable | --unstable | --verbatim] < <patch>
| Name | Description |
|---|---|
--stable |
Hash each file's diff separately and add them up, so the order of files in the patch does not matter. This is the recommended mode. |
--unstable |
Hash the whole patch in one stream, which depends on file order. This was the original behavior. |
--verbatim |
Do not ignore whitespace when hashing. Implies --stable. |
Output: one line per patch found in the input: <patch-id> <commit-id>. The commit ID is taken from the commit line that git show and git log -p print; for a bare git diff the second column is a string of zeros.
Edge cases: an empty patch or a patch without hunks (such as a pure rename or a merge commit) produces no output line. Merge commits in a git log -p stream have no patch of their own, so they are skipped.
Usage notes
- What is ignored. Line numbers in
@@headers, the commit message, the author, dates and (by default) whitespace are left out of the hash. The file names and the changed and context lines are included. - Context matters. Two changes with the same added lines but different context lines get different IDs. A cherry-pick onto code that differs near the change can therefore stop matching.
- Stable versus unstable. The config variable
patchid.stablechooses the mode when no flag is given, so pass--stableexplicitly in scripts. Mixing IDs created by the two modes never matches, so store which mode you used if you keep IDs in a database. - Not a security hash. Use it for matching changes, not for proving integrity.
git cherryandgit log --cherry-pickcompute patch IDs for you: see git log and git rebase.- Binary files have no textual diff; their patches hash identically only if the binary patch blocks are the same.
Related
- git log, git show, git rebase, git revert
- Lessons: Cherry-pick, Rebase