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

GitVersion ControlMergeDiffGit Diff

Git Problem Overview


I want a diff of all changes in a branch that is not merged to master yet.

I tried:

git diff master
git diff branch..master
git diff branch...master

However, in each of these cases the diff contains content in master that has not been merged into my branch yet.

Is there a way to do a diff between my branch and master that excludes changes in master that have not been merged into my branch yet?

Git Solutions


Solution 1 - Git

git diff `git merge-base master branch`..branch

Merge base is the point where branch diverged from master.

Git diff supports a special syntax for this:

git diff master...branch

You must not swap the sides because then you would get the other branch. You want to know what changed in branch since it diverged from master, not the other way round.

You may want to replace branch in this syntax with HEAD or even delete it completely -- both the following display the content of the current branch since it diverged from master:

git diff master...HEAD
git diff master...

Loosely related:


Note that .. and ... syntax does not have the same semantics as in other Git tools. It differs from the meaning specified in man gitrevisions.

Quoting man git-diff:

> * git diff [--options] <commit> <commit> [--] [<path>…] > > This is to view the changes between two arbitrary <commit>. > > * git diff [--options] <commit>..<commit> [--] [<path>…] > > This is synonymous to the previous form. If <commit> on one side is omitted, it will have the same effect as using HEAD instead. > > * git diff [--options] <commit>...<commit> [--] [<path>…] > > This form is to view the changes on the branch containing and up to the second <commit>, starting at a common ancestor of both <commit>. "git diff A...B" is equivalent to "git diff $(git-merge-base A B) B". You can omit any one of <commit>, which has the same effect as using HEAD instead. > > Just in case you are doing something exotic, it should be noted that all of the <commit> in the above description, except in the last two forms that use ".." notations, can be any <tree>. > > For a more complete list of ways to spell <commit>, see "SPECIFYING REVISIONS" section in gitrevisions[7]. However, "diff" is about comparing two endpoints, not ranges, and the range notations ("<commit>..<commit>" and "<commit>...<commit>") do not mean a range as defined in the "SPECIFYING RANGES" section in gitrevisions[7].

Solution 2 - Git

Here's what worked for me:

git diff origin/master...

This shows only the changes between my currently selected local branch and the remote master branch, and ignores all changes in my local branch that came from merge commits.

Solution 3 - Git

As also noted by John Szakmeister and VasiliNovikov, the shortest command to get the full diff from master's perspective on your branch is:

git diff master...

This uses your local copy of master.

To compare a specific file use:

git diff master... filepath

Output example:

Example usage

Solution 4 - Git

According to Documentation

> git diff Shows changes between the working tree and the index or a > tree, changes between the index and a tree, changes between two trees, > changes resulting from a merge, changes between two blob objects, or > changes between two files on disk.

In git diff - There's a significant difference between two dots .. and 3 dots ... in the way we compare branches or pull requests in our repository. I'll give you an easy example which demonstrates it easily.

Example: Let's assume we're checking out new branch from master and pushing some code in.

  G---H---I feature (Branch)
 /
A---B---C---D master (Branch)
  • Two dots - If we want to show the diffs between all changes happened in the current time on both sides, We would use the git diff origin/master..feature or just git diff origin/master
    ,output: ( H, I against A, B, C, D )

  • Three dots - If we want to show the diffs between the last common ancestor (A), aka the check point we started our new branch ,we use git diff origin/master...feature,output: (H, I against A ).

  • I'd rather use the 3 dots in most circumstances.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpillarOfLightView Question on Stackoverflow
Solution 1 - GitPalecView Answer on Stackoverflow
Solution 2 - GitJeshurunView Answer on Stackoverflow
Solution 3 - GitAndrew SchreiberView Answer on Stackoverflow
Solution 4 - GitavivamgView Answer on Stackoverflow