Git diff between current branch and master but not including unmerged master commits

Learn git diff between current branch and master but not including unmerged master commits with practical examples, diagrams, and best practices. Covers git, version-control, merge development tech...

Git Diff: Comparing Your Branch to Master (Excluding Merged Commits)

Hero image for Git diff between current branch and master but not including unmerged master commits

Learn how to effectively use git diff to compare your current branch with the master branch, specifically excluding commits that have already been merged into master. This ensures you only see changes unique to your work.

When working on a feature branch, it's common to want to see the differences between your current work and the main master branch. However, a simple git diff master might show changes that have already been integrated into master from other branches, or even changes from master that you've pulled into your branch. This can make it difficult to isolate the specific changes you've introduced.

This article will guide you through using git diff commands to precisely compare your current branch against master, focusing only on the commits unique to your branch that have not yet been merged into master. This is crucial for code reviews, preparing for merges, and understanding your contributions.

Understanding the Problem: Simple git diff master

A straightforward git diff master command compares the tip of your current branch with the tip of the master branch. While this might seem like what you want, it often includes changes that are already present in master but not yet in your branch, or changes that you've pulled from master into your branch. This can lead to a noisy diff that doesn't accurately represent your unmerged work.

Consider a scenario where master has advanced since you branched off, and you've also pulled master into your feature branch. A direct git diff master would show all changes between the two branch tips, including those from master that you've incorporated, and potentially changes from other branches that landed on master.

Hero image for Git diff between current branch and master but not including unmerged master commits

Simple git diff master showing all differences between branch tips.

The Solution: Using git diff with Merge Bases

To get a diff that truly represents the changes unique to your current branch (i.e., commits on your branch that are not yet on master), you need to compare your branch against the point where it diverged from master. This point is known as the 'merge base'. The merge base is the most recent common ancestor commit between two branches.

By comparing your current branch with its merge base relative to master, you effectively filter out any changes that master has introduced since your branch diverged, or changes you've pulled from master into your branch. This gives you a clean diff of only your new, unmerged work.

git diff master...HEAD

The most common and recommended command for this scenario.

Let's break down git diff master...HEAD:

  • master: Refers to the master branch.
  • HEAD: Refers to the tip of your current branch.
  • ... (three dots): This syntax tells Git to compare the tip of your current branch (HEAD) with the merge base of master and HEAD. It then shows the differences introduced only on your current branch since that merge base.

This command is equivalent to git diff $(git merge-base master HEAD) HEAD but is much more concise and idiomatic.

Hero image for Git diff between current branch and master but not including unmerged master commits

Using git diff master...HEAD to show only unique changes on the feature branch.

Viewing the Commits, Not Just the Diff

Sometimes, you might want to see the list of commits that are unique to your branch and not yet on master, rather than just the file differences. This is useful for understanding the history you're about to merge.

git log master..HEAD

List commits on current branch not on master.

The git log master..HEAD command (two dots) shows all commits reachable from HEAD that are not reachable from master. This effectively lists all commits that are unique to your current branch and have not yet been merged into master.

Practical Steps for Daily Workflow

Here's how you can integrate these commands into your daily Git workflow:

1. Step 1: Ensure your local master is up-to-date

Before comparing, always make sure your local master branch reflects the latest state of the remote master. This ensures an accurate merge base calculation.

git checkout master
git pull origin master
git checkout your-feature-branch

2. Step 2: View the diff of your unique changes

From your feature branch, run the three-dot diff command to see only the changes you've introduced that are not yet on master.

git diff master...HEAD

This is ideal for a final review before creating a pull request or merging.

3. Step 3: Review the unique commit history

If you want to see the individual commits that constitute these unique changes, use the two-dot log command.

git log master..HEAD

This helps you understand the commit history you're bringing into master.