28

I want to see the difference between the master branch and my feature branch. I have many pulls from the master to my feature branch and want to see the changes that would be added if I merged my feature into the master.

This is my situation:

-*--*--*-----*<master>
  \     \     \
   1--*--*--*--2--*<feature>

My problem is the git diff master feature seems to only display commit number 2. I want to see the diff that a github pull request would show, which I believe is all the way to commit 1. I noticed git cherry shows me the commits I want to see the difference for.

Thanks for any advice.

Matthieu
  • 15,277
  • 10
  • 54
  • 83
codr
  • 854
  • 3
  • 11
  • 17

3 Answers3

140

The important thing to realize about git diff A B is that it only ever shows you the difference between the states of the tree between exactly two points in the commit graph - it doesn't care about the history. The .. and ... notations used for git diff have the following meanings:

An illustration of the different ways of specifying commits for git diff

So when you run git diff master feature that's not just showing you the change introduced by the commit you've marked as 2 - the output should show the exact differences between the state of the tree commited in master and the state of the tree commited in feature. If it's not showing you the earlier changes on your feature branch, perhaps you resolved conflicts from the earlier merges from master in favour of the version in master?

As cebewee says it may be that what you want is git log -p master..feature, since git log does care about history. The meaning of .. and ... for git log are different since they select a range of commits:

An illustration of the different ways of specifying ranges of commits for git log

Incidentally, its often said that merging from master into a topic branch is the wrong thing to do - instead you should be rebasing, or merging your topic branch into master after it is complete. This keeps the meaning of the topic branch easily understood. The git maintainer did a (somewhat difficult to understand) blog post about the philosophy of merging which discusses that.

Community
  • 1
  • 1
Mark Longair
  • 385,867
  • 66
  • 394
  • 320
  • 2
    However, the linked blog post does say that in a situation in which the feature branch no longer merges cleanly into `master` one correct course of action is to merge `master` into the feature branch and resolve conflicts on the feature branch. – Plasma Jul 29 '15 at 00:17
  • Thank you for the link to the philosophy of merging. I was reading that and I could not find out the name of the author, just that he is the maitainer. Do you know his name? -- Edited: never mind. His name is Junio C Hamano. Tx – DrBeco Apr 25 '17 at 00:58
  • Nice answer. Shame about the insane syntax! – lost May 31 '19 at 16:18
6

git diff master feature does not show any of the commits' but the textual difference between the commits master and feature. It sounds as if you want to see all commits from feature, which are not yet in master? In this case, try git log master..feature or git log -p master..feature, if you want to see the diffs, too.

See the section SPECIFIYING RANGES in man git-rev-parse for an explanation of the 'a..b' syntax.

Lars Noschinski
  • 3,597
  • 14
  • 29
2

I'm relatively new to git, but if I understand your question correctly. Your question is rooted from not properly understanding remote and local repositories and how they relate to one another. I remember once I understood it, everything became 2X easier.

You think you are at this situation below where you have only 2 branches:

  • featureBranch
  • masterBranch

Yet if you do a git branch -a you will be able to see all of your branches, local and remote.

So your real situation is:

  • featureBranch
  • masterBranch
  • remoteBranch(s)
  *---* <remote's Master> which is behind your local
      \
      -*--*--*-----*<master> which is ahead of your remote/origin
       \     \     \
        1--*--*--*--2--*<feature>

In order for you to be able to see the diff similar to what you see in Github Pull Request you must diff it with your remote branch.

git diff <local branch> <remote>/<remote branch>

See compare local git branch with remote branch?

Community
  • 1
  • 1
Honey
  • 24,125
  • 14
  • 123
  • 212