Git: How to compare changes between branches

Updated: January 27, 2024 By: Guest Contributor Post a comment

Introduction

Git is a distributed version control system widely used in software development to track changes in source code. It allows multiple developers to work on a project without stepping on each other’s toes. A fundamental aspect of working with Git is managing branches. When you’re working on a project, it’s common to have several branches, each representing a different line of development, feature, or release. Therefore, comparing branches to understand the changes is a crucial skill for developers.

In this tutorial, we’ll explore how to compare changes between branches using Git, with examples ranging from basic to advanced usage. By the end of this guide, you will have a solid foundation on how to examine the differences between branches in a Git repository.

Setting Up a Git Repository

Before diving into comparing branches, make sure you have Git installed on your system. You’ll also need a Git repository for practicing. You can create a new Git repository or clone an existing one. Here’s how to initialize a new Git repository:

git init my-repo
cd my-repo

Creating Branches

To demonstrate how to compare branches, we’ll need at least two branches. Here’s how to create a new branch:

git branch new-feature
git checkout new-feature
# Or with newer versions of Git:
git checkout -b new-feature

Basic Branch Comparison

To compare the current branch to another branch, you can use:

git diff other-branch-name

This command shows the diff output of what’s different in the ‘other-branch-name’ compared to the current branch you’re on. This output will show additions as lines prefixed with a ‘+’ and deletions as lines prefixed with a ‘-‘.

Comparing Branch Tips

If you want to see the differences at the last commit of each branch (the ‘tips’), you can specify the branches explicitly:

git diff branch1..branch2

For instance, to compare the ‘main’ branch to your ‘new-feature’ branch, use:

git diff main..new-feature

Comparing Specific Files between Branches

Sometimes, you only want to see differences in specific files between two branches:

git diff branch1..branch2 -- path/to/file

Replace ‘path/to/file’ with the file’s path you are interested in.

Checking Branch Divergence

To see how many commits each branch has that the other doesn’t, use the following:

git rev-list --left-right --count branch1...branch2

This will return a pair of numbers: the first is the number of commits unique to ‘branch1’, and the second is the number of commits unique to ‘branch2’.

Advanced Comparisons

For a more sophisticated comparison, the git log command is powerful. This shows you the commits unique to the target branch alongside each commit’s diff.

git log branch1..branch2

To see the diffs introduced with each commit during the comparison, you can add the -p option:

git log -p branch1..branch2

If you’re interested in a particular file or directory, you can include it after the branch names:

git log -p branch1..branch2 -- path/to/file

Viewing History with a Graphical Representation

Using git log with additional options can render a more visual representation of the branch history:

git log --oneline --graph --decorate --all

This command provides a compact view of the commit history, organized in a graph, showing where branches diverged and merged.

Using GUI Tools for Comparison

In addition to the command-line tools provided by Git, there are various graphical user interface (GUI) Git clients that offer visual comparison tools. These can be easier for those who prefer a more visual approach to code comparison.

Conclusion

Comparing branches in Git is a fundamental aspect of navigating and understanding the codebase within a version-controlled environment. Starting with basic git diff, to more advanced techniques with git log, and even GUI tools, these skills will aid in effective collaboration and code review processes. By mastering branch comparisons, developers can maintain a clear overview of their project’s evolution and ensure that integrating changes from different development streams is as smooth and error-free as possible.