开发者

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

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 show开发者_开发知识库s 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?


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.


The existing answer, instead of showing modifications only in files that exist in both commits, shows a diff of all modified files in both commits.

Say, for example, one commit had two files modified and another has 1000. According to the question, the diff should contain two files or less, but the other solution shows 1002 files in the diff!


Working Solution

I couldn't find any existing git command to do this, hence I had to use a combination of existing commands.

First, let's define an alias files that gives us the names/paths of all files in a given commit like so -

git config --global alias.files 'diff-tree --no-commit-id --name-only -r'

Now git files commit_1 gives us all files present in commit_1 and doing the same on commit_2 gives us all files present in commit_2. Please see this answer of why/how this command works.

Now, some command-line acrobatics -

sort <(git files commit_1)  <(git files commit_2) | uniq -d | xargs -d '\n' -n1 git diff commit_1 commit_2 > my_changes.diff

We are passing the list of files present in both commits to sort and then picking files common to both by passing the -d flag to uniq which picks only duplicates and finally invoking git diff on these common files via xargs and saving our diff into a file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜