Namely, I am interested in finding files that were modified in both commits (or in two diffs if that is easier). Excuse me if it's simple, but I am still pretty new to git.
-
Why do you need this? Maybe there is a better way to solve your problem. – michas Feb 25 '14 at 22:02
-
1We are interested in finding changes that may have been lost in a large commit. – Paul Becotte Feb 25 '14 at 22:12
2 Answers
Update
After inputs from @PaulHicks ( git diff
instead of git diff-tree
...thanks Paul) and to circumvent the error described here i.e | sort
here's my updated answer:
comm -12 <(git diff --no-commit-id --name-only -r 3fe29472016343 | sort) <(git diff --no-commit-id --name-only -r 9fd796b1998bb7d5 | sort)
Old Answer I dont believe there is a command out of the box to do what you want to achieve. However, If you are on *nix platforms you can try this:
comm -12 <(git diff-tree --no-commit-id --name-only -r COMMIT1SHA1key) <(git diff-tree --no-commit-id --name-only -r COMMIT2SHA1key)
unfortunately I am typing from windows box :( ..hence cannot check this right away (I have to go home and try)
My idea here is git diff-tree will produce a list of files like here.
and this link shows how this comm -12 works on ls
.
Weaving both may work is my guess
-
1I really just came across this on Stack Overflow...see those links in my answer – Vikram Feb 25 '14 at 21:46
-
@PaulHicks thanks for letting me know...I unfortunately cannot check this right now as I am on a windows machine...I will go home and try it out – Vikram Feb 25 '14 at 21:54
-
`<(...)` is a bash-ism: run the parenthesized command with its stdout going to a pipe, replace the `<(...)` part with a `/dev/fd/` ref to the pipe descriptor. It's a handy trick for avoiding temporary files. – torek Feb 25 '14 at 21:55
-
Note that `git diff-tree` with one commit-ID implicitly compares the tree with the commit's parent(s), and behaves differently (produces a combined diff) for a merge commit. – torek Feb 25 '14 at 21:59
-
No I wasn't asking anything. I just tried the syntax from the answer and I got no output for a commit that modified one file; the git diff approach from my answer give me the file that was changed. It's probably just a minor syntax thing. – Paul Hicks Feb 25 '14 at 21:59
-
Ah yes, the SHA-1 I used was a merge. diff-tree works fine for other SHA-1s. The diff approach works in all cases though, which is handy. I guess it depends on what you want from the command. – Paul Hicks Feb 25 '14 at 22:02
You can get the list of files changed in a single commit using git diff --name-only sha1^ sha1
. So you could do this in a script which takes two commit SHA-1s as parameters:
git diff --name-only $1^ $1 | sort > file1
git diff --name-only $2^ $2 | sort > file2
comm -12 file1 file2
rm file1 file2
Or if you're using bash on linux/unix, you can use the nifty trick from @Vikram's answer to avoid the temporary files:
comm -12 < (git diff --name-only $1^ $1) < (git diff --name-only $2^ $2)

- 13,289
- 5
- 51
- 78