A reliable Git diff and patch workflow helps you see exactly what changed, review it in context, share only the necessary edits, and apply fixes without disturbing unrelated work. This guide covers the commands to compare working trees, branches, commits, and files; explains when to use a patch or cherry-pick; and provides recurring checkpoints for code review, backports, hotfixes, and releases.
Overview
Git offers several ways to inspect and transport changes, but the commands answer different questions. git diff shows differences between states. git log explains the sequence of commits. git show examines one commit in detail. git format-patch turns commits into email-style patch files, while git apply applies file changes without creating a commit. git cherry-pick copies an existing commit, including its message and author information, onto another branch.
The safest workflow starts with a precise comparison and ends with a verification step. Before changing anything, identify the source and target states, check whether your working tree is clean, and decide whether you need a review artifact, an uncommitted change, or a new commit. That distinction prevents a common mistake: using a command that moves more history than intended when a small patch would be easier to inspect.
git status
git diff
git diff --stat
git diff --check
git diff --check can help reveal whitespace errors in the changes being inspected. It does not replace tests or review, but it is a useful low-cost checkpoint before sharing a patch.
| Command | Best used for | Important distinction |
|---|---|---|
git diff | Comparing working-tree, staged, branch, or commit content | Shows content differences; normally does not create history |
git log | Reviewing commit order, messages, and ancestry | Explains history rather than displaying every line change |
git show | Inspecting one commit, tag, or object | Combines commit metadata with its patch by default |
git format-patch | Exporting one or more commits as reviewable patch files | Represents commits and can preserve their sequence |
git apply | Applying a patch to files in the working tree | Does not create a commit automatically |
git cherry-pick | Applying an existing commit to the current branch | Creates a new commit with the selected change |
What to track
Working-tree and staged changes
Use git diff for edits that are not staged and git diff --cached for the proposed next commit. Reviewing both views matters because staging divides the work into two states. A file can contain changes that are already staged and additional edits that are not.
# Unstaged changes
git diff
# Staged changes
git diff --cached
# Names and summary only
git diff --name-status
git diff --stat
For a focused review, name the path after --:
git diff -- src/components/Button.css
This is especially useful in frontend work, where generated files, lockfiles, and formatting changes can otherwise obscure the functional edit. If the difference includes structured data or queries, a consistent formatter can make intent easier to review. The SQL formatter and query review workflow provides a similar discipline for database changes.
Comparing branches and commits
To compare two branch tips, use two explicit references:
git diff main...feature/login
git diff main feature/login
These commands answer different questions. The three-dot form compares the feature branch with the common ancestor it shares with main, which is often useful for reviewing what a branch introduces. The two-dot form compares the two endpoint trees directly. State the notation in your review notes so another developer knows which baseline was used.
To inspect the commits that are present on one branch but not another, use a range with git log:
git log --oneline main..feature/login
git log --oneline --graph --decorate main..feature/login
For one commit, git show is usually clearer than reconstructing the same information from a broad diff:
git show --stat <commit>
git show --format=fuller <commit>
Review artifacts and patches
Use a plain diff when the recipient needs to inspect content changes. Use git format-patch when you need to export commits as a sequence that can be reviewed or transported:
git format-patch -1 <commit>
git format-patch main..feature/login --output-directory patches
A patch file should be accompanied by context: the source revision, target branch, expected behavior, test commands, and any known limitations. Avoid treating a patch as self-explanatory, particularly when it changes configuration, migrations, deployment files, or generated output.
File paths, renames, and binary content
Git can detect likely renames and copies when comparing content, but that detection is a presentation aid rather than a guarantee that the filesystem operation was recorded in a particular way. Review both the old and new paths when a rename is reported. Check references in imports, build scripts, documentation, deployment manifests, and case-sensitive paths.
Binary files generally do not produce a useful line-by-line diff. Track the path, file type, size or checksum when appropriate, and the application-level effect. A binary patch may require repository configuration or a separate delivery process. Do not assume that a visually small diff means a binary change is safe.
Cadence and checkpoints
Git review is not a one-time activity. Use a repeatable checkpoint at each stage of work rather than waiting until release day.
Before starting or switching tasks
- Run
git statusand identify uncommitted work. - Record the branch and intended comparison target.
- Separate unrelated edits before creating a patch or commit.
- Confirm that ignored, generated, and local configuration files are not being mistaken for deliverable changes.
Before opening a review
- Inspect staged content with
git diff --cached. - Review the commit list with
git log --oneline. - Run
git diff --checkand the project’s relevant tests or validation commands. - Summarize behavior changes separately from refactoring, formatting, or file movement.
Before applying a patch
- Save or commit unrelated local work.
- Confirm the patch’s intended base and inspect its file list.
- Test whether it applies cleanly with
git apply --check patch.diff. - Apply it, inspect the resulting diff, and run validation before committing.
git apply --check patch.diff
git apply --3way patch.diff
git diff
git status
Three-way application can use repository history to resolve context that is no longer exact, but it can still produce conflicts or an incorrect result if the source and target have diverged substantially. Treat a successful application as a starting point for review, not proof that the change is correct.
Monthly or quarterly maintenance
For active repositories, add a recurring workflow review monthly or quarterly. Look for large commits that combine unrelated concerns, repeated conflict patterns, stale backport procedures, patches that lack test instructions, and generated files that routinely overwhelm diffs. Refresh team examples when Git versions, hosting workflows, repository conventions, or deployment practices change.
How to interpret changes
A useful diff answers three questions: what changed, why it changed, and what could be affected outside the edited lines. Start with the file summary, then inspect the full hunks. A compact summary can hide a risky change in a configuration file, migration, permission rule, or build step.
When whitespace obscures the review, use whitespace-aware options carefully:
git diff --ignore-space-change
git diff --ignore-all-space
git diff --word-diff
These options can expose intent in a heavily formatted file, but they can also hide meaningful indentation or spacing changes. Review the normal diff as well when whitespace affects syntax or rendering.
For a patch workflow, the choice between git apply and git cherry-pick depends on what you are transporting. Choose git apply when you want file changes without automatically creating a commit, or when the patch is a standalone artifact. Choose git cherry-pick when a specific existing commit is the unit you want to bring into another branch and preserving its commit-level context is useful. Neither option removes the need to inspect conflicts and test the result.
When conflicts occur, use this checklist:
- Stop and identify every conflicted path with
git status. - Read the surrounding code, not only the conflict markers.
- Check whether a rename, delete, or case-only path change is involved.
- Review whitespace and line-ending differences separately from logic changes.
- For binary files, choose the intended version deliberately rather than attempting line-based resolution.
- Run focused tests first, then the broader project checks.
- Inspect the final diff and confirm that no unrelated edits were included.
Keep review notes concrete. A reusable template can include: Baseline, Compared revision, Files of interest, Behavior changed, Tests run, Known risks, and Follow-up required. This format makes a later backport or incident review easier.
When to revisit
Revisit this Git diff guide whenever a repository changes its branching model, review platform, release process, or patch-delivery method. It is also worth returning before a hotfix, a long-lived feature merge, a major refactor, or a backport to a maintenance branch. The comparison target may look familiar while the repository’s history and generated files have changed underneath it.
At a monthly or quarterly checkpoint, sample recent reviews and ask:
- Are reviewers comparing the correct branch or merge base?
- Do commits mix formatting, generated output, and behavior changes?
- Are patches accompanied by enough context to apply safely?
- Do conflict resolutions receive focused tests?
- Are renamed paths and binary assets being verified explicitly?
Before your next change, use this short sequence: run git status, define the two states you are comparing, inspect the appropriate diff, check whitespace, verify paths and generated files, and record the test result. If you are transporting work, choose between git apply and git cherry-pick based on whether the desired unit is a file change or an existing commit. That small discipline makes Git code review more predictable and reduces the chance of overwriting unrelated work.