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.