Git
Version Control
Commit Differences
Programming
Software Development

How to see the changes between two commits without commits in-between?

Interview Questions practice on Codemia

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

Browse interview questions

Viewing the changes between two specific commits in a version-controlled project is vital for developers to understand the evolution of code and to pinpoint the introduction of specific changes or bugs. This is especially useful in large projects where multiple contributions are merged frequently. Below, we explore different methods to achieve this using Git, a popular Version Control System (VCS).

Using git diff

The primary tool for comparing commits in Git is git diff. This command is versatile and can be used to compare different states in Git, including differences between commit histories, the working directory, branches, etc.

Basic Syntax and Usage

The basic syntax to compare two commits is:

 
git diff <commit1> <commit2>

Here, <commit1> and <commit2> are either commit hashes or references to commits (like HEAD, or branch names).

Example

Consider you want to compare the state of a repository at two different points. You can use their commit hashes:

 
git diff a1b2c3d4 e5f6g7h8

Understanding the Output

The output of git diff includes lines added or removed between these commits. Lines removed from <commit1> are prefixed with a -, and lines added to <commit2> are prefixed with a +.

For a clearer division, use the --color-words option to show changes at the word level instead of the whole line:

 
git diff --color-words a1b2c3d4 e5f6g7h8

Using Git Range Notation

Git supports range notations to simplify the process of comparing changes between two non-adjacent commits.

Double Dot .. Syntax

This is another straightforward approach:

 
git diff <commit1>..<commit2>

This will show all differences from <commit1> to <commit2>. If you use this in a branch comparison, like comparing main and feature branches (main..feature), it shows all changes that are in the feature branch since it diverged from main.

Triple Dot ... Syntax

The triple dot syntax is useful when comparing branches:

 
git diff <branch1>...<branch2>

This command compares the tips of each branch to their common ancestor, showing changes that occurred on either branch since they diverged.

Summary Table

Command or NotationUse CaseDescription
git diff <commit1> <commit2>Direct comparison of two commitsShows exact code differences between two commits
git diff <commit1>..<commit2>Range comparison using double dotsDiff of cumulative changes between two points
git diff <branch1>...<branch2>Comparison using triple dots for branchesShows what changes have occurred on either branch since divergence

Additional Tools and Options

Beyond basic comparisons, Git provides several tools and options for a more detailed or focused comparison:

  • Patches: git format-patch can be used to create downloadable and printable text files of changes between commits.
  • Git GUIs: Tools like GitKraken or SourceTree provide a graphic interface for comparing commits which can be more user-friendly, especially for those who prefer not to use the command line.
  • Visualizing with Diffs: Many IDEs and editors (like VSCode, Atom) have built-in or extendable functionality to visually parse diff outputs, enhancing readability and understanding.

Conclusion

Understanding how to compare two commits effectively using Git commands provides developers with the power to track and understand changes across a project's history, helps in reviewing code and assists in debugging issues introduced in prior changes. Using the methodologies discussed, developers can gain detailed insights into the evolution of a project.


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.