2

Consider the following Git graph:

* 2ac0064 (HEAD -> master) Add lorem ipsum to Section 4
| * 9784cf5 (feature/B) Add " world" to Section 1
| * 9e32334 Add "hello" to Section 1
|/  
* c2f6030 Add lorem ipsum to Section 3
| * fbc5b46 (feature/A) Add " world" to Section 1
| * 66674d3 Add "lo" to Section 1
| * 96b8620 Add "hel" to Section 1
|/  
* 79c6f39 Add some section headers
* a41b867 Initial commit

As can bee seen, both feature/A and feature/B introduce the exact same changes (adding "hello world" to Section 1). However, they are separated by c2f6030 which makes unrelated changes to Section 3. How would I go about diffing only the changes introduced by feature/A with the changes introduced by feature/B?


git diff feature/A feature/B produces the following diff, which is wrong, since I do not care about the unrelated changes introduced by c2f6030 in Section 3.

$ git diff feature/A feature/B
diff --git a/hello.txt b/hello.txt
index 789bbdc..5bc57c1 100644
--- a/hello.txt
+++ b/hello.txt
@@ -8,5 +8,7 @@ hello world
 
 ## Section 3
 
+Lorem ipsum dolor sit amet
+
 ## Section 4
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Felix ZY
  • 674
  • 6
  • 14
  • Suppose `master` has set `hello.txt` to `hello world` and neither B1 nor B2 does anything further to that file. And suppose A3 has set `hello.txt` to `hello world`. Well then, `hello.txt` is `hello world` in both `feature/A` and `feature/B`. So what should the answer be? – matt May 23 '21 at 01:09
  • I'm confused: why can't you just diff the two branches? – Kraigolas May 23 '21 at 02:49
  • 2
    I agree with @Kraigolas. `git diff` does exactly what you described. Did you try it and what was the issue? – The Dreams Wind May 23 '21 at 02:53
  • I've rewritten the question with actual git output and an explanation of why a straight `git diff feature/A feature/B` is insufficient. The issue is that `git diff feature/A feature/B` includes the unrelated changes from `master` which I'm not interested in. – Felix ZY May 23 '21 at 09:56
  • @matt I do not care about changes made on master - only changes made in the branches. If neither B1 or B2 does anything further to `hello.txt` I would expect to see `hello world` when diffing the two branches. – Felix ZY May 23 '21 at 09:58
  • The problem is that Git has no way of knowing (by itself) that you do not consider `c2f6030` to be part of `feature/B`, because from Git's point of view it *is* part of `feature/B`. – mkrieger1 May 23 '21 at 10:06
  • @mkrieger1 Is there no way around this though? It is possilble to view the changes introduced by a single commit - so why not a range of commits? And why not the range of commits since the branch point from master? And why should it not then be possible to compare one such range of changes to another range of changes? – Felix ZY May 23 '21 at 10:09
  • Does this answer your question? [Is there a way to compare two diffs or patches?](https://stackoverflow.com/questions/8569699/is-there-a-way-to-compare-two-diffs-or-patches) – mkrieger1 May 23 '21 at 10:09
  • @mkrieger1 `git range-diff` from that quesiton looks promising. I'll look into it – Felix ZY May 23 '21 at 10:13
  • 2
    If I understand correctly from [this answer](https://stackoverflow.com/a/52512813), you should be able to use `git range-diff 79c6f39..feature/A c2f6030..feature/B`. I'm sure there are ways to determine `79c6f39` and `c2f6030` programmatically. – mkrieger1 May 23 '21 at 10:13
  • Right, yes; `master..feature/X` seems to be the way to resolve the range start. I was a bit uncertain whether to use `..` or `...` but `..` seems to be the way to go. Would you like to write a concrete answer @mkrieger1 or should I? – Felix ZY May 23 '21 at 10:23

0 Answers0