'git diff': Show only diff for files that exist in both commits

git

Solution

The following may do what you want:

git diff --diff-filter=M commitA commitB

The `M` option to `--diff-filter` says only to include files that appear to be modified between the two commits - ones that only exist in one branch or the other would be selected with `A` ("added") or `D` ("deleted").

You can see which files would be selected with these letter codes by doing:

git diff --name-status commitA commitB

... and there's more information about all of that in the `git diff` documentation.

Problem

Is there a way to use `git diff` to get a diff between two commits, but only show the diff for the files that exist in both commits? I have a branch I created a couple of weeks ago, and our main code has diverged quite a bit from it by now. As a result, if I do a diff between my current HEAD and the tip of the old branch, I get dozens of changed files, but it's mostly just noise. I really want to see a diff that shows only the files that exist in both branches. I know one way to do this would be to cherry-pick the other branch's commits on top of the current HEAD, but is there a way to do it just using `git diff`?

Original source

Related problems