19

While working on a branch that I've opened a pull request on, I would like to see the exact same diff that GitHub displays on the commandline. What is the particular git diff command that replicates the list of changes GitHub displays for a pull request?

Jacob Dalton
  • 1,643
  • 14
  • 23
  • Possible duplicate of [How to make git diff show the same result as github's pull request diff?](http://stackoverflow.com/questions/37763836/how-to-make-git-diff-show-the-same-result-as-githubs-pull-request-diff) – João Antunes Apr 06 '17 at 14:59
  • This question is 2 years older than the one you linked to. So the other is the duplicate ;) – stason Sep 27 '20 at 04:20

6 Answers6

9

The closest thing is to diff with the common ancestor between your feature-branch and your base-branch.

Something like:

git diff `git merge-base feature-branch base-branch`
pic
  • 433
  • 6
  • 8
4

If you don't want to do any git fetch and update a local PR branch, you simply can use cli/cli, the command-line interface for GitHub.

The release 0.9.0 includes gh pr diff

https://user-images.githubusercontent.com/98482/82266353-13a03300-992f-11ea-8d71-2a0995707f87.png

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
2

Not sure if there is a way to get the actual diff format closer to Github style but $ git diff master...<branch_name> seems to show the set of changes that a pull request would show (assuming it's a pull request against master). The list of changed files in a pull request seems to be equivalent to $ git diff --name-status master...<branch_name>. I guess this all assumes that your local branches are up-to-date with the remote Github branches

helixbass
  • 46
  • 4
1

Assuming that you want to check against the base branch called master:

git diff $(git merge-base --fork-point master)

This will give all the changes since the branch was made, including yet uncommitted changes!

This command gives the same results as the one in @pic's answer, but it's a bit simpler as it doesn't require knowing the feature-branch.

If you want only the committed changes, similar to gh pr diff command as in @VonC's answer, the following command will give the same results:

git diff $(git merge-base --fork-point master) HEAD

The git diff way approach could be more useful in some situations, since you have all the other git diff features available. For example, if you don't want the diff, but instead want to know which files were modified, you can just pass the --name-only:

git diff --name-only $(git merge-base --fork-point master)

which you can't do with gh.

stason
  • 5,409
  • 4
  • 34
  • 48
  • These commands don't give me the same output as `gh pr diff`. They give me every file that's changed between the feature branch and the master branch, i.e. if there have been other merges to `master` since the fork was created, they'll be listed as well – ashgromnies Jul 22 '21 at 03:14
  • Right, I found issues with this too since I first wrote it. Try: `git diff $(git merge-base master HEAD)` instead. I should probably update this answer. Let me know if it worked. – stason Jul 22 '21 at 05:13
0

See the Reviewing and Synchronising section in GitHub advanced training. In particular, after fetching the pull request you can view the diff prior to merging:

$ git fetch origin refs/pull/1/head
$ git show FETCH_HEAD
EricM
  • 462
  • 3
  • 5
  • 1
    Is there a way to do this without pulling down the refs? This seems like workflow/annoyance. I would prefer to use a set of commands completely using the git commandline without fetching the extra refs created by GitHub. Like `git diff ...master + flags`. – Jacob Dalton Sep 27 '14 at 22:40
-1

git diff branchA branchB should work, no?

More info on other perhaps useful diff notations can be found here

Community
  • 1
  • 1
João Antunes
  • 811
  • 9
  • 16