主题分支上的 Git diff,排除同时发生的合并提交?
假设我有以下情况:
B---D---F---G topic
/ /
--A---C---E master
出于代码审查的目的,我想从提交 A 中提取差异到提交 G,但不包括发生在 master 分支上的提交 E 和 C,也不包括提交 F合并提交。
换句话说,我想生成一个包含从 F 到 G 的更改的差异,并将这些更改与从 A 到 D 的更改聚合。
换句话说,我希望审查差异仅包含我对主题分支的更改,不包括同时发生的来自 master 的一堆代码。
这可能吗?如果 git 无法处理这样的“差异聚合”,如果有人可以提供一些关于外部命令如何做到这一点的指示(这样我就可以尝试编写一个可以解决问题的 bash 脚本),我将非常感激。
Let's say I have the following situation:
B---D---F---G topic
/ /
--A---C---E master
For code review purposes, I would like to pull out a diff from commit A to commit G, but not including commits E and C which happened on the master branch, and also not including commit F which is a merge commit.
In other words, I would like to generate a diff that contains changes from F to G and aggregate those changes with changes from A to D.
In other-other words, I want the review diff to contain only my changes from the topic branch, not including a bunch of code from master that happened in the meantime.
Is this possible? If git cannot handle such "diff aggregations", I would be very thankful if someone can provide some pointers as to how some external command can do that (so that I can try writing a bash script which would do the trick).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Karl 的答案具有误导性:
git diff
永远不会在正确的范围内(即多次提交)工作,只能在两次提交之间工作!范围语法对于 git diff 具有特殊含义。
git diff E..G
始终等于git diff E G
(并且在这种情况下,
git diff E...G
等于git diff E G
,因为 E 是两者之间的合并基础。git diff master...topic
是您在这个和一般情况中想要的:它向您显示主题分支的所有更改,并将其与上次合并进行比较 - base(即将它与上次合并时的 master 进行对比,忽略 master 中所有后来不在您的主题分支中的更改)。注意:不幸的是(巧合?)
git log
等中x..y
的效果最好用x来表示...y
在git diff
中,反之亦然!The answer from Karl is misleading:
git diff
never works on proper ranges (i.e. multiple commits), only between two commits!The range syntax has special meaning for
git diff
.git diff E..G
is always equal togit diff E G
(andgit diff E...G
is equal togit diff E G
in this case, because E is the merge-base between the two).git diff master...topic
is what you want in this and in the general case: it shows you all changes of the topic branch, comparing it to the last merge-base (i.e. contrasting it to master when it was merged the last time, ignoring all later changes in master which are not in your topic branch anyway).Note: unfortunately (by coincidence?) the effect of
x..y
ingit log
, etc. is best represented byx...y
ingit diff
and vice versa!如果您不关心差异的“上下文”部分中的内容,那么 git diff master..topic 将为您提供您想要的内容。
C
和E
中引入的任何更改都被视为 diff 的“基础”的一部分,因此来自master
的更改将出现在上下文,但只有在主题分支中所做的实际更改才会用+
或-
标记为更改。这通常是人们想要进行代码审查的内容。相反,如果您想像合并从未发生过一样,则必须将其变基:
If you don't care what's in the "context" part of the diffs, then
git diff master..topic
will give you what you want. Any changes introduced inC
andE
are considered part of the "base" of the diff, so the changes frommaster
will be there in the context, but only the actual changes made in your topic branch will be marked with+
or-
as changes. That's usually what people want for code reviews.If instead you want to act like the merge never happened, you will have to rebase it out:
怎么样:
git checkout topic
git checkout -b temp
git revert F
git diff A
然后你就可以愉快地删除临时分支了。
What about:
git checkout topic
git checkout -b temp
git revert F
git diff A
Afterwards you can happily remove the temp branch.