Git Diff and Patch Workflow: Compare Changes, Create Patches, and Resolve Conflicts
gitversion-controlpatchescode-reviewcherry-pickdeveloper-workflow

Git Diff and Patch Workflow: Compare Changes, Create Patches, and Resolve Conflicts

CCode Craft Hub Editorial Team
2026-11-09
6 min read

A practical Git checklist for comparing branches, creating and applying patches, cherry-picking commits, and resolving conflicts safely.

Git gives you several ways to inspect, share, and transfer changes, but each command fits a different job. This checklist explains how to compare files, commits, and branches with git diff, create portable patches with git format-patch, apply changes safely, use cherry-pick for selected commits, and review rewritten commit series with git range-diff.

Overview

A reliable Git patch workflow starts by identifying what you are comparing and how the result will be used. A diff is usually the right output for inspection or review. A patch file is useful when changes must be sent outside the normal branch workflow. A cherry-pick transfers an existing commit into another branch while preserving its commit message and metadata as Git applies the change. These choices are related, but they are not interchangeable.

TaskCommandBest use
Inspect working-tree changesgit diffReview unstaged edits before adding them
Inspect staged changesgit diff --stagedCheck exactly what the next commit contains
Compare branches or commitsgit diff base..topicSee the net file changes between references
Create a commit patchgit format-patchExport one or more commits as email-style patch files
Apply a plain diffgit applyApply file changes without creating a commit automatically
Transfer selected commitsgit cherry-pickBring specific commits onto the current branch
Compare two commit seriesgit range-diffReview how a rebased or revised series changed

Before running a command, record the source and destination clearly. The source might be a working tree, a commit, or a branch. The destination might be a review, another branch, a release branch, or a separate repository. This simple distinction prevents many accidental applications of the wrong change set.

Checklist by scenario

Compare uncommitted, staged, and committed work

Use these commands to narrow a review to the correct part of the repository:

# Unstaged changes in tracked files
git diff

# Changes already staged for commit
git diff --staged

# Changes introduced by one commit
git show --stat COMMIT
git show COMMIT

# Compare two commits or references
git diff OLDER_COMMIT NEWER_COMMIT

# Compare the tip of two branches
git diff main..feature

# Compare a branch with its common ancestor
git diff main...feature

The two-dot and three-dot forms answer different questions. main..feature compares the two named tips. main...feature compares the common ancestor with the feature tip, which is often a better representation of what a feature branch adds relative to its starting point. Confirm the references with git log --oneline --decorate --graph --all when the branch history is unfamiliar.

Create a Git patch for selected commits

Use git format-patch when the commit boundaries, messages, and authorship should travel with the change:

# Export the last commit to a directory
git format-patch -1 HEAD --output-directory patches

# Export commits after a base through the current branch tip
git format-patch BASE_COMMIT..HEAD --output-directory patches

# Export a numbered series with a readable subject prefix
git format-patch --cover-from=auto --subject-prefix="PATCH" BASE_COMMIT..HEAD

Inspect the generated files before sharing them. Check that the range excludes unrelated commits, that the patch series applies in the intended order, and that generated files or secrets are not included. A patch created from commits is different from a raw diff: the former represents a commit series, while the latter primarily represents file-level changes.

Apply a patch without committing immediately

For a plain diff or a patch whose commit metadata is not required, test and apply it with git apply:

# Check whether a patch can be applied cleanly
git apply --check change.patch

# Apply the patch to the working tree
git apply change.patch

# Apply while recording rejected hunks for manual repair
git apply --reject change.patch

# Review the result
git diff

Keep the working tree clean where possible. If you have unrelated edits, the patch may still apply, but the resulting review becomes harder to reason about. After applying the patch, run the relevant formatter, tests, or build commands before committing.

Apply a commit with cherry-pick

Use cherry-pick when a complete existing commit needs to be transferred to the current branch:

# Review the commit first
git show --stat COMMIT
git show COMMIT

# Apply one commit
git cherry-pick COMMIT

# Apply several specific commits in an intentional order
git cherry-pick FIRST_COMMIT SECOND_COMMIT

# Stop and inspect if a conflict occurs
git status

Cherry-pick is common for backporting a focused fix to a maintenance branch. Confirm that the selected commit does not depend on earlier commits from its original branch. If it does, transfer the prerequisite commits or choose a smaller, self-contained fix.

Review a revised commit series

When a contributor rebases or updates a series after review, compare the old and new ranges with:

git range-diff OLD_BASE..OLD_TIP NEW_BASE..NEW_TIP

This helps identify whether a requested change was added, removed, reordered, or altered between two versions of the series. It is more informative than comparing only the final branch tips when commit structure matters during review.

What to double-check

  • References: Verify branch names and commit IDs before comparing or applying anything. A short ID can be ambiguous in a large repository.
  • Direction: Read the command as a source-to-destination operation. For a diff, confirm which side is the baseline and which side contains the proposed change.
  • Scope: Use a pathspec when only certain files belong in the result, for example git diff main..feature -- src/.
  • Whitespace: Use git diff --check to catch trailing whitespace and simple patch formatting issues before sharing changes.
  • Binary files: Do not assume a text patch fully represents binary changes. Confirm how the repository manages generated or binary assets.
  • Dependencies: A small-looking commit may rely on configuration, migrations, tests, or earlier refactors. Inspect its parent history and surrounding changes.
  • Validation: After applying changes, review the diff again and run the narrowest useful tests before running a wider test suite.
  • Security: Scan patches and diffs for credentials, private URLs, local paths, debug output, and environment-specific settings before sharing them.

Common mistakes

Comparing the wrong range. A branch comparison can include changes that are not part of the feature if the chosen base is incorrect. Find the merge base or intended release point first.

Using a raw diff when commit history matters. A raw diff can transfer file content, but it does not preserve the original sequence of commits. Use format-patch or cherry-pick when that history is important.

Applying a patch to a dirty working tree. Unrelated local edits make conflicts and review errors more likely. Commit, stash, or separate unrelated work before applying a patch.

Continuing after a conflict without checking status. During a cherry-pick, inspect conflicted files, resolve only the intended hunks, stage the resolutions, and then run git cherry-pick --continue. If the operation is no longer appropriate, use git cherry-pick --abort to return to the pre-operation state.

Assuming successful application means correctness. Git can apply a patch cleanly even when surrounding code has changed semantically. Review the resulting code and test behavior, not just the absence of conflict markers.

When to revisit

Return to this checklist before release backports, recurring maintenance work, large code reviews, or any transfer between branches maintained by different teams. Recheck the workflow when your repository changes its branching model, when a hosting platform changes its comparison interface, or when your team adopts new merge, rebase, or patch-review conventions.

Before acting, use this short sequence:

  1. Identify the exact source and destination.
  2. Inspect the relevant commits with git log and git show.
  3. Choose a diff, patch, cherry-pick, or range-diff based on whether you need content, commits, or series history.
  4. Test the operation with --check or a dry review where available.
  5. Apply the change on an appropriately clean branch.
  6. Review the resulting diff, resolve conflicts deliberately, and run relevant validation.

That sequence keeps Git comparison and patch work observable: you know what changed, why it is being transferred, and what must be verified afterward.

Related Topics

#git#version-control#patches#code-review#cherry-pick#developer-workflow
C

Code Craft Hub Editorial Team

Developer Tools Editor

Senior editor and content strategist. Writing about technology, design, and the future of digital media. Follow along for deep dives into the industry's moving parts.