0

Whenever you perform a pull request, you get a nice diff of the changes of this PR, with the master.

I want to be able to save these PRs - these diffs actually, in order to study a particular topic.

However, it is impossible to save the html page to either html, or pdf properly.

So with a help from a member on this site, i found diff2html, which exports a diff to html.

Using this answer: How to make git diff show the same result as github's pull request diff?

I did git diff feature/BRANCH_I_AM_INTERESTED_IN...master | diff2html -i stdin -F diff.html

However, this gives me many many results. It includes many many files that were changed at a much later time - not at the time that the PR was merged.

I guess what this does is a comparison with the latest version of this branch, to the master as it is in the current time.

However, a PR diff is the comparison of the branch with the master at the past time.

So, is there a way to get the diff of the PR - in other words, get the diff with the branch and the master but at the time the PR was merged?

user1584421
  • 3,499
  • 11
  • 46
  • 86
  • Does this answer your question? [Is there a quick way to "git diff" from the point or branch origin?](https://stackoverflow.com/questions/29810331/is-there-a-quick-way-to-git-diff-from-the-point-or-branch-origin) – mkrieger1 Feb 17 '22 at 14:36
  • I think your diff has the branches backwards. This will be *closer* to what you want: `git diff master...feature/BRANCH_I_AM_INTERESTED_IN`. – TTT Feb 17 '22 at 17:14
  • For historic PRs that were *completed*, can't you just compare the merge commit of the PR with it's first parent? That should show you exactly what that PR brought in. – TTT Feb 17 '22 at 17:16
  • I'm guessing the combination of my two comments probably makes an answer. LMK if that's true and I'll write it up with more details. – TTT Feb 17 '22 at 17:30
  • @mkrieger1 Thank you for your help. But the truth is, because i am newbie at git, i could not fully understand the answer. – user1584421 Feb 18 '22 at 11:25
  • @TTT Thank you very much! Due to my low skill level at git, i could not understand your method. Especially your second comment. Is it easy to provide the commands for your second comment as well? Or create an answer with the required commands? Sorry for the trouble. – user1584421 Feb 18 '22 at 11:28

1 Answers1

1

First off, let's fix the syntax of this command:

git diff feature/BRANCH_I_AM_INTERESTED_IN...master

The reason you observed:

However, this gives me many many results. It includes many many files that were changed at a much later time...

is because you have the branches flipped. When you use the 3 dot syntax with diff, it shows all changes on the right side since where the branches split. (Here's a quick cheat sheet for dot syntax covering both diff and log.)

The command you need, in order to see the changes in a PR to merge branch feature/BRANCH_I_AM_INTERESTED_IN into master should be:

git diff master...feature/BRANCH_I_AM_INTERESTED_IN

However, that may not be exactly what you want! That will probably work most of the time, but it doesn't work in the scenario where some of the changes on feature/BRANCH_I_AM_INTERESTED_IN already reside on master too. For example, maybe a bug fix on your feature branch was so important that it had to be cherry-picked over to master to make a production hotfix ASAP. Because of this, behind the scenes the diff you see in the context of a PR may be different than the obvious 3 dot syntax diff. Since PR tools (GitHub, GitLab, AzureDevops, BitBucket, etc) are independent of Git itself, it's even possible that the diff you see varies depending on the tool you're using!

Important Note: using the branch names in the diff command, specifically master, only works before the PR is completed. If you want to use the same command after the PR is completed, you'd need to change master to the commit master was pointing to before completion.

That being said, fortunately for the context of your question, once the PR is completed, it's really straight-forward to see what the exact changes were that the PR brought in without the diff command. (Note this does suggest that if you compare the diff after the PR was completed, and that differs from what the PR tool showed you before it was completed, then the PR tool was actually "wrong", or at least "misleading", and this is unfortunately pretty common!)

To see what changes a PR brought in, you simply need to diff the merge commit of a PR, with it's first-parent. Typically you could do this with with a 2 dot diff:

git diff <commit-id-of-PR-merge-commit>~1..<commit-id-of-PR-merge-commit>

(Note you should put the parent commit first in the diff command so you see it from the correct POV. If you flip them new files will appear as deletes, etc.)

Now, what if you don't have a merge commit for the PR? That would happen if the branch to be merged in was already up to date with master, and if a fast-forward merge was allowed meaning no merge commit was created. In that case it's a little trickier because you would have to know the commit master was on before that PR was completed. I hope that whatever PR tool you use would be able to provide that information, in a historical context, so that you can compare the two commits on master representing before and after the PR was completed.

TTT
  • 22,611
  • 8
  • 63
  • 69