53

Sometimes I have the following problem:

  1. Add some commits in a feature branch.

  2. Update master from upstream.

  3. Want to see the diff between the feature branch and master, but git diff master shows all of the things that have been added/removed in master, when I really only want to see the commits that the feature branch is ahead of master, not the ones that it's behind.

Is there a way to do this without having to merge master into the feature branch?

mwhite
  • 2,041
  • 1
  • 16
  • 21
  • 1
    This question doesn't make it clear about whether you want to see ***commits*** that `feature` has that `master` doesn't have, or whether you only want to see ***the diff*** of changes made in the feature branch that haven't been made in `master`. –  Jun 12 '14 at 14:31
  • Possible duplicate of [Using Git, show all commits that are in one branch, but not the other(s)](https://stackoverflow.com/questions/1710894/using-git-show-all-commits-that-are-in-one-branch-but-not-the-others) – 0 _ Sep 09 '17 at 05:11

5 Answers5

75

I think the right answer is triple dot

git diff master...feature

That shows only new changes on feature with respect to master. See also: git diff - show only what's new on the remote

Community
  • 1
  • 1
Danny Roberts
  • 3,442
  • 23
  • 28
  • 15
    It also works if your on the feature branch and you do `git diff master...` omitting the feature branch name :) – lee penkman Jun 30 '16 at 05:55
38

I usually go for

git log -p feature-branch --not master

But if you want a diff, you're looking for

git diff <common-ancestor> master

where <common-ancestor> is a common ancestor of your feature branch and master that you want to compare to. You can use merge-base to find that for you.

git diff `git merge-base feature-branch master` feature-branch
Peter Lundgren
  • 8,787
  • 2
  • 26
  • 21
  • 1
    Won't this show the commits on Master after the branch point and not the changes on the feature-branch? – gtrig Jul 12 '13 at 00:28
  • 1
    Using `git log master --not ` without the `-p` I obtained the commits without the diff. – Rubens Mariuzzo Oct 26 '15 at 18:39
  • In case if you want to have whole diff for commits which are in the branch, but not in the `master` you can also use `git diff master^ feature-branch` or even just `git diff master^` - this recipe will work even for Windows, where cmd.exe has no backticks. – Andrey Starodubtsev Mar 13 '23 at 12:22
34

Your question is a little unclear because I'm not sure if you want to see a diff/patch of the changes between master and feature, or you just want to see which branches contain which commits that the other branch doesn't have.

Assuming that you just want to see what commits are in your feature branch that don't exist in master (i.e. which commits in feature are "ahead" of master), then you can do

$ git log --oneline --graph master..feature

If you want to see how both master and feature have diverged, you can use this command:

$ git log --oneline --graph --first-parent \
--decorate --left-right master...feature
> 5bef654 (feature) A ...
> f7c65ea B ...
> fc66245 C ...
> 7435fc0 D ...
< bf68204 (master) X ...
< 0c10ed1 Y ...
< 27bb774 Z ...

The above output shows commits in master that are not in feature with < in front of them (since you used master...feature with master on the left < side of the command), while commits in feature that aren't in master are marked with > since you used feature on the right side of master...feature. The triple dots ... in this form are important, don't leave them out.

You can learn more about specifying Commit Range from the Pro Git book.

0

If I understand your question correctly, your feature branch is off of the master branch, and you want to see the differences between what you have committed on the feature branch and what the master branch looked like when you branched from it.

A----B----C
 \
  D----E----F

In that case you can use use the git diff command with the corresponding hashes of the commits. So for a feature branch that branched at point A and has subsequently committed D, E, and F, you can do this:

git diff <hash of commit F> <hash of commit A>

You can use git merge-base feature master to see the hash of point A to use above, and git log to see the hash for your most recent commit, F, on the feature branch. Or to do it all on one line use this command:

git diff feature `git merge-base feature master`
gtrig
  • 12,550
  • 5
  • 28
  • 36
0

Not an exact answer, but a one liner that works most of the time for my needs.

git difftool -d master..

akras14
  • 76
  • 3