Git
Version Control
Commits
Code Comparison
Software Development

Show diff between commits

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Showing diffs between commits in Git is a core workflow for code review, debugging regressions, and release auditing. While git diff syntax is straightforward, choosing the right range and output format matters for accurate analysis. Developers often compare the wrong endpoints or miss rename/move context, leading to incorrect conclusions about what changed.

Core Sections

Basic commit-to-commit diff

Use commit hashes directly.

bash
git diff <old_commit> <new_commit>

This shows changes needed to go from old to new snapshot.

Useful options

Limit noise and improve readability:

bash
1git diff --stat <old> <new>
2git diff --name-only <old> <new>
3git diff -w <old> <new>
4git diff -M <old> <new>

-M helps detect renames; -w ignores whitespace-only changes.

Compare ranges with two-dot and three-dot

  • A..B often used in log-style commit ranges.
  • A...B for diff compares merge-base(A,B) to B, useful for branch review.
bash
git diff main...feature

This is common in pull-request preparation.

Diff specific file between commits

bash
git diff <old> <new> -- path/to/file.py

Great for targeted investigations in large repositories.

Visual and external tools

You can use git difftool or IDE integrations for side-by-side analysis when line-level context is complex.

Common Pitfalls

  • Comparing the wrong commit order and misreading added vs removed lines.
  • Confusing log range semantics with diff endpoint semantics.
  • Ignoring rename detection and treating moved files as full delete/add.
  • Reviewing huge diffs without narrowing to relevant paths first.
  • Using whitespace-sensitive diffs for formatting-only commits.

Implementation Playbook

To make this topic production-ready, treat implementation as a repeatable workflow instead of a one-time fix. Start by defining an explicit baseline with known inputs, expected outputs, and measured runtime behavior. Baselines are critical because many regressions appear only after dependency upgrades, environment changes, or infrastructure shifts that do not modify application code directly. A baseline lets you detect drift quickly and determine whether a failure came from logic changes, runtime configuration, or platform behavior.

Next, design a small but representative validation matrix that covers happy-path, edge-case, and failure-path scenarios. Keep the matrix lightweight enough to run frequently, ideally in local development and CI, and strict enough to catch common integration mistakes. If this topic depends on external services, include deterministic stubs or contract fixtures so tests remain stable and actionable. For observability, log key identifiers, decision branches, and outcome statuses in a structured format; this allows fast correlation in dashboards and incident timelines without manual guesswork.

After correctness checks, add operational safeguards. Define timeout behavior, retry policy, and rollback triggers before rollout. Avoid making multiple high-risk changes simultaneously; apply one change, verify, then continue. Incremental rollout minimizes blast radius and produces clearer diagnostics when behavior diverges from expectations. In shared systems, publish a short runbook that lists prerequisites, expected metrics, and first-response troubleshooting steps. This documentation prevents repeated rediscovery work and improves handoff quality across teams.

Use the following execution checklist for consistent delivery:

text
11. Capture baseline behavior and expected outputs
22. Run happy-path, edge-case, and failure-path tests
33. Validate environment and dependency compatibility
44. Record structured logs and key performance metrics
55. Roll out incrementally with clear rollback criteria
66. Update runbook notes with observed outcomes

Change Control Note

Apply updates in small increments and verify each increment with one deterministic test run before proceeding. Incremental changes reduce rollback scope and make root-cause analysis faster if behavior shifts after dependency or configuration changes.

Summary

git diff between commits is simple but requires correct endpoint selection and options. Use range forms intentionally, narrow by path when needed, and enable rename/whitespace options for cleaner reviews. This produces faster and more accurate change analysis.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.