最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

git - How can I do a simple diff between two commits, uninfluenced by another other commits? - Stack Overflow

programmeradmin1浏览0评论

I thought I was beginning to understand git diff.

But today I did a git diff between a commit at the tip of one branch ("A") with a commit at the tip of another ("B"), where B is a newer branch and tip of B is a newer commit than that at the tip of A. Thus I'm doing this:

> git diff [sha of tip of A] [sha of tip of B] > output_diff.txt

I'm quite baffled by the results, and in particular by the fact that lines which are present in neither commit's version of a given file (call it xxx.py) are included in the diff, with "++" at the start of the line.

These are all lines which were in branch B in the version of that file at one point ... but were then DELETED before the branch B tip commit was made. I'm baffled as to why these are there. They are of no interest to me, and make the diff file all the more difficult to understand.

Is there any way to just exclude these lines when doing a git diff? I.e. a diff based on these two commits completely in isolation.

I looked at the various diff options. Nothing jumped out as possibly doing this.

Edit: use case

My use case is that, starting from the tip of a TDD branch, I made a spur branch, what I call "wild coding", i.e. coding with no TDD. After a few commits on that I now want to see, very simply, what the differences are between the files as they are at the tip of the "wild coding" branch and the files as they are at the tip of the TDD branch, i.e. where the "wild branch" starts.

The idea being to then continue TDD development, taking inspiration from the experimental/wild coding I did in the spur branch. I quite often do this in fact for particular situations, where I feel a need to explore coding possibilities without TDD constraints.

I'm now assuming I don't really understand the purpose of git diff. I'm assuming now that I'll simply have to checkout the wild branch, copy the directory containing the app files, and then checkout the TDD branch again. And then use a 3rd party "diff/comparison" tool to compare the state of the app files as they are in the the two respective places on the commit tree. I had assumed that git diff could be used for that purpose. It seems that I have misunderstood.

It seems there are two (at least) forms of git diff format: "unified" and "combined". "Combined" seems to have two characters ("+", "-" or space) at the start of lines, whereas "unified" seems to have one such. I don't know whether this might give me what I'm after ... because I can't currently work out how to deliver a git diff using "unified" format.

I thought I was beginning to understand git diff.

But today I did a git diff between a commit at the tip of one branch ("A") with a commit at the tip of another ("B"), where B is a newer branch and tip of B is a newer commit than that at the tip of A. Thus I'm doing this:

> git diff [sha of tip of A] [sha of tip of B] > output_diff.txt

I'm quite baffled by the results, and in particular by the fact that lines which are present in neither commit's version of a given file (call it xxx.py) are included in the diff, with "++" at the start of the line.

These are all lines which were in branch B in the version of that file at one point ... but were then DELETED before the branch B tip commit was made. I'm baffled as to why these are there. They are of no interest to me, and make the diff file all the more difficult to understand.

Is there any way to just exclude these lines when doing a git diff? I.e. a diff based on these two commits completely in isolation.

I looked at the various diff options. Nothing jumped out as possibly doing this.

Edit: use case

My use case is that, starting from the tip of a TDD branch, I made a spur branch, what I call "wild coding", i.e. coding with no TDD. After a few commits on that I now want to see, very simply, what the differences are between the files as they are at the tip of the "wild coding" branch and the files as they are at the tip of the TDD branch, i.e. where the "wild branch" starts.

The idea being to then continue TDD development, taking inspiration from the experimental/wild coding I did in the spur branch. I quite often do this in fact for particular situations, where I feel a need to explore coding possibilities without TDD constraints.

I'm now assuming I don't really understand the purpose of git diff. I'm assuming now that I'll simply have to checkout the wild branch, copy the directory containing the app files, and then checkout the TDD branch again. And then use a 3rd party "diff/comparison" tool to compare the state of the app files as they are in the the two respective places on the commit tree. I had assumed that git diff could be used for that purpose. It seems that I have misunderstood.

It seems there are two (at least) forms of git diff format: "unified" and "combined". "Combined" seems to have two characters ("+", "-" or space) at the start of lines, whereas "unified" seems to have one such. I don't know whether this might give me what I'm after ... because I can't currently work out how to deliver a git diff using "unified" format.

Share Improve this question edited Mar 12 at 19:15 mike rodent asked Mar 12 at 18:11 mike rodentmike rodent 15.8k14 gold badges120 silver badges195 bronze badges 9
  • stackoverflow/a/23527631/2442804 also read through the second best answer as an explanation of why this makes little sense in the context of git itself. – luk2302 Commented Mar 12 at 18:21
  • 1 Do you have an example GitHub repo you could link to demonstrate the problem? – ruohola Commented Mar 12 at 18:21
  • 2 I see a lot of prose, extraordinary claims—the command you say you ran showing lines in neither tip as added? Really?—and no evidence. – jthill Commented Mar 12 at 22:52
  • 1 Diff marks lines that were added with a leading "+". "combined diffs" give the marks for both tips, so the only diff markup that would start "++" would be a line added on both tips. But you say the line doesn't exist at at least one of the tips, prose is inexact but there's no sane read of what you said was being diffed that would produce the marks you say are there. – jthill Commented Mar 12 at 23:43
  • 1 you do want git diff, or possibly git difftool (or git difftool -d for a directory view). You may want to also use --diff-filter, check git help diff for its usage. – LeGEC Commented Mar 13 at 6:36
 |  Show 4 more comments

1 Answer 1

Reset to default 0

XY problem? Or just the wrong tool for the job?

This might prove useful to someone. It seems that what I really want is the BASH command diff (available in Cygwin or WSL in a Windows context).

  • check out and copy the newer commit (or a relevant directory of it) to a temporary directory (here D:/temp/core)

  • check out the older commit

  • do something like this:

>diff -Nru src/core d:/temp/core > temp.diff

Options: N: new files also included; r: recursive; u: "unified" format

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论