git diff Command: Detect Changes in CI
git diff reports the differences between commits, the index, and the working tree.
In CI, diff is less about reading patches and more about answering yes-or-no questions: did anything change, which paths changed, and is the tree clean? Its flags make it scriptable.
Common flags
--name-only- list just the changed file paths--name-status- list paths with their status letter (A/M/D)--stat- show a per-file summary of insertions and deletions--exit-code- exit nonzero when there are differences (zero when clean)--quiet- like --exit-code but suppress output<from>..<to>- diff between two commits or branches
Example
shell
# Fail the job if a generated file drifted from source
make generate
if ! git diff --exit-code -- generated/; then
echo "Generated files are stale; run make generate" >&2
exit 1
fiIn CI
git diff --exit-code is the classic check that codegen, formatting, or lockfiles are committed: if the tree changed after running a generator, the job fails. Use --name-only to scope later steps (build only changed packages) and --stat for readable PR summaries.
Key takeaways
- git diff --exit-code gates CI on whether generated or formatted files are committed.
- --name-only feeds change-aware steps that act on just the modified paths.
- Compare branches with from..to to scope a diff to a pull request.
Related guides
git status Command: Check Tree State in CIgit status shows working tree and index state. Reference for --porcelain, the stable machine-readable format…
git show Command: Inspect Objects in CIgit show displays commits, tags, and file contents at a revision. Reference for printing a commit message or…
git apply Command: Apply Patches in CIgit apply applies a patch to the working tree or index. Reference for --check, --3way, and --index used to ap…
git log Command: Inspect History in CIgit log shows commit history. Reference for --oneline, -n, and --format to extract commit messages and author…