主题分支上的 Git diff,排除同时发生的合并提交?

发布于 2024-12-05 13:48:01 字数 391 浏览 1 评论 0原文

假设我有以下情况:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白芷 2024-12-12 13:48:02

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来表示...ygit 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 to git diff E G (and
git diff E...G is equal to git 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 in git log, etc. is best represented by x...y in git diff and vice versa!

薆情海 2024-12-12 13:48:02

如果您不关心差异的“上下文”部分中的内容,那么 git diff master..topic 将为您提供您想要的内容。 CE 中引入的任何更改都被视为 diff 的“基础”的一部分,因此来自 master 的更改将出现在上下文,但只有在主题分支中所做的实际更改才会用 +- 标记为更改。这通常是人们想要进行代码审查的内容。

相反,如果您想像合并从未发生过一样,则必须将其变基:

git rebase --onto A 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 in C and E are considered part of the "base" of the diff, so the changes from master 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 rebase --onto A master
_畞蕅 2024-12-12 13:48:02

怎么样:

  1. git checkout topic

  2. git checkout -b temp

  3. git revert F

  4. git diff A

然后你就可以愉快地删除临时分支了。

What about:

  1. git checkout topic

  2. git checkout -b temp

  3. git revert F

  4. git diff A

Afterwards you can happily remove the temp branch.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文