Git diff between current branch and master but not including unmerged master commits
Solution 1
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 usingHEAD
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 usingHEAD
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 ingitrevisions[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 ingitrevisions[7]
.
Solution 2
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
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:
Solution 4
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 justgit diff origin/master
,output: (H, I
againstA, 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 usegit diff origin/master...feature
,output: (H, I
againstA
). -
I'd rather use the 3 dots in most circumstances.
Related videos on Youtube
pillarOfLight
Updated on July 08, 2022Comments
-
pillarOfLight almost 2 years
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?
-
John Szakmeister over 10 yearsIf you flip around the second version, you get what you want:
git diff master..branch
. You can shorten it togit diff master..
if you're on branch. Ther1..r2
syntax is short for^r1 r2
which means "show me everything that descends fromr2
and is not reachable fromr1
".git help gitrevisions
has information on the various syntaxes you can use. -
Palec over 9 yearsI expanded my answer after I read more on the
...
syntax ofgit diff
. Your comment is wrong, @jszakmeister, because revision ranges as described ingitrevisions
have nothing to do withgit diff
. Diff compares two points in history, cannot work with a range. -
John Szakmeister over 9 yearsYou are correct. I always forget that
git diff
works differently than the other commands... a fact that I find frustrating. :-( -
joe almost 4 yearsensure that you update the local copy of master before comparing
-
-
Joel Peltonen over 7 yearsFor me
$ git diff master...branch
producedfatal: ambiguous argument 'master...branch': unknown revision or path not in the working tree.
- is this a version dependent command? -
Joel Peltonen over 7 yearsActually, I just realized that "branch" must be the name of your branch, I thought it was a reference to the current branch
-
Palec over 7 yearsYou are right, my answer relies on the branch being called
branch
. I chose to stick with the name the OP had chosen in the question. If you want to use current branch, replacebranch
withHEAD
. -
VasiliNovikov over 7 yearsNote that you can use
git diff master...
to avoid specifying the branch (the current one will be taken). -
Palec over 7 yearsYes,
HEAD
(the current branch) is implicit, @VasyaNovikov. The same holds in many other places. -
ChrisGuest almost 7 years
git diff devel...bugfix/API-353-api-allows-database-access-when
fatal: ambiguous argument 'devel...bugfix/API-353-api-allows-database-access-when': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
-
ChrisGuest almost 7 yearsThis answer is not working for me with
git version 2.11.0 (Apple Git-81)
-
ChrisGuest almost 7 yearsOK. I've figured this out. I obviously need to checkout the branch as this is a local comparison.
git checkout devel
-
Palec almost 7 yearsDoes the original command work after you checked out
devel
, @ChrisGuest? Probably, Git created the branch for you during checkout, as a local copy of a remote branch (typicallyorigin/devel
). If that was the case,git diff origin/devel...bugfix/API-353-api-allows-database-access-when
would have worked even before the checkout. -
ChrisGuest almost 7 yearsYep the original command worked after I checked out
devel
. Thanks for pointing this out - I will do probably create a bash function that doesgit diff origin/devel...$1
as it is a very useful command when I am coding. -
jaytibann almost 5 yearsFor reference, if you need the commit refs of commits that contain these changes, use
git cherry origin/master
. -
s.meijer almost 5 years
git diff origin/develop...HEAD
, works perfectly! Thanks. -
Michael over 4 yearsIf this shows a bunch of garbage you didn't expect,
master
may have rebased a set of commits out from under you. -
DrCord almost 3 yearsSo helpful, thorough and clear, I wish I could upvote this twice! Thanks for helping me improve my git understanding, this was very useful in a pipeline to test/deploy based on what changed when a feature branch merges into dev.
-
will almost 3 yearsThis really works -- I found that I needed a visual
diff
tool for all the files that my compare gave me. I recommend this Answer ongit diff
withmeld
-- stackoverflow.com/a/24967334/108350, if you want to use adiff
GUI. -
Cerin almost 2 yearsWhen op wrote "current branch", they didn't literally mean a branch named "branch"...
-
Palec almost 2 years@Cerin, that is hard to tell. :-) I wrote my answer so that it corresponds to the examples the OP has provided. Your comment, the top-voted comment, and another one hint about really using the current branch -- I included the info in my answer.