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.
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:
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:
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:
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:
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:
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 Notation | Use Case | Description |
git diff <commit1> <commit2> | Direct comparison of two commits | Shows exact code differences between two commits |
git diff <commit1>..<commit2> | Range comparison using double dots | Diff of cumulative changes between two points |
git diff <branch1>...<branch2> | Comparison using triple dots for branches | Shows 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-patchcan 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
- How to see the changes between two commits without commits in-between?
- How to set offset committed by the consumer group using Spark's Direct Stream for Kafka?
- How to set patience as default git diff algorithm
- How to set specific Java version to Maven?
- How to show full-file Git blame in Visual Studio Code
- How to show Git log history i.e., all the related commits for a sub directory of a Git repository
- How to show the first commit by 'git log'?
- How to show uncommitted changes in Git and some Git diffs in detail
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.