Skip to content

git diff quick reference ​

Algorithms ​

CommandUse
git diff --diff-algorithm=histogramNoisy refactors, boilerplate files
git diff --diff-algorithm=patienceCode reordering, shifted functions
git diff (default = myers)Fast, general use

Set globally: git config diff.algorithm histogram

CI Gates ​

bash
git diff --quiet HEAD~1 -- src/ && echo "skip build" || echo "build needed"
git diff --cached --check                    # pre-commit whitespace check
git diff --name-only HEAD~1 HEAD | grep -q 'infra/' && terraform plan

Rename/Copy ​

bash
git diff --find-renames=80%                  # tighten threshold
git diff --find-copies-harder                # full copy detection (slow)
git diff --no-renames --name-only            # disable, fast

Programmatic Output ​

bash
git diff --numstat HEAD~1 HEAD               # added  deleted  file
git diff --shortstat HEAD~1 HEAD             # summary line
git diff --name-only HEAD~1 HEAD             # file paths only
git diff --name-status --diff-filter=A       # only new files
git diff --raw HEAD~1 HEAD                   # compact, status codes

Diff Filter Codes ​

A(dded) C(opied) D(eleted) M(odified) R(enamed) T(ype changed) U(nmerged) X(unknown) -- lowercase = exclude.

Comparing ​

bash
git diff main...develop                      # changes on develop since branch
git diff v2.3.0:vendor/lib.js v2.4.0:vendor/lib.js   # specific file across tags
git diff --cached                            # staged vs HEAD
git diff                                     # unstaged vs HEAD (after add)

Merge Diffs ​

bash
git show <merge-sha>                         # combined diff
git diff-tree --cc -p <merge-sha>            # CI audit of merge changes

Performance ​

bash
git diff-tree --no-commit-id -r --name-only HEAD~1 HEAD   # fastest tree diff
git diff --no-textconv                       # skip binary text conversion
git config diff.renameLimit 5000             # raise rename detection ceiling

Word Diff ​

bash
git diff --word-diff HEAD~1 -- docs/*.md
git diff --word-diff-regex='[^[:space:],.-]+'

Exit Codes ​

0 = no diff, 1 = diff found, >1 = error.

bash
git diff --quiet HEAD~1 HEAD && echo "clean" || echo "changes"

Git Master — Git Reference for Every Skill Level