Git 比较两个分支与第三个分支的关系

Git compare two branches in relation to a third

与 github/gitlab 合作,在许多情况下使用功能分支,拉取请求在等待审查时会过时,需要在一段时间后重新基于新的目标分支(master)。

这有时会产生需要解决的冲突。

变基后,我想比较与目标分支相关的旧分支和新分支,以验证在解决冲突时没有出现错误。意思是我想验证合并新版本的 PR 后哪些行会发生变化。

所以基本上我想比较 git diff branch origin/mastergit diff origin/branch origin/master 的结果——新旧版本之间 github/gitlab 的 PR 视图上的差异输出发生​​了什么变化。

是否有执行此操作的命令?

是的,你可以使用git-diff的形式,即takes multiple commits:

git diff <commit> <commit>…​ <commit>

来自documentation

This form is to view the results of a merge commit. The first listed must be the merge itself; the remaining two or more commits should be its parents.

在你的例子中,你想比较变基的 branchorigin/master 之间的合并提交与旧的 origin/branchorigin/master 之间的合并提交;然后比较变成:

git diff branch origin/master origin/branch

其中 branch 是“合并提交”,origin/masterorigin/branch 是它的“parents”。

如果发生冲突,Git 将产生以下差异:

- line in origin/master (the first "parent" commit)
- line in origin/branch (the second "parent" commit)
+ line in branch (the merge commit)