我试图在GitHub上审查一个拉请求到一个不是主的分支。目标分支在master后面,拉请求显示了来自master的提交,所以我合并了master并将其推送到GitHub,但刷新后,他们的提交和差异仍然出现在拉请求中。我已经再次检查了GitHub上的分支是否有来自master的提交。为什么它们仍然出现在拉请求中?

我还检查了本地拉请求,它只显示未合并的提交。


当前回答

这发生在GitHub中,当你压缩从目标分支合并的提交时。

I had been using squash and merge with Github as the default merge strategy, including merges from the target branch. This introduces a new commit and GitHub doesn't recognize that this squashed commit is the same as the ones already in master (but with different hashes). Git handles it properly but you see all the changes again in GitHub, making it annoying to review. The solution is to do a regular merge of these pulled in upstream commits instead of a squash and merge. When you want to merge in another branch into yours as a dependency, git merge --squash and revert that single commit before pulling from master once that other branch has actually made it to master.

编辑:另一种解决方案是重新基底和强制推。干净但被改写的历史

其他回答

看起来Pull Request没有跟踪目标分支的变化(我联系了GitHub支持,并在2014年11月18日收到了回复,说这是故意的)。

但是,你可以通过以下方法让它显示更新后的更改:

http://githuburl/org/repo/compare/targetbranch...currentbranch

根据需要替换githuburl, org, repo, targetbranch和currentbranch。

或者正如hexsprite在他的回答中所指出的,你也可以通过点击PR上的Edit来强制它进行更新,并临时将基础更改为不同的分支,然后再返回。这会产生警告:

你确定要改变底色吗? 类中的旧基本分支中的一些提交可能会被删除 时间轴,旧的评论可能会过时。

并将在PR中留下两个日志条目:

对于遇到这种情况并对GitHub Pull Request行为感到困惑的其他人来说,根本原因是PR是源分支尖端与源分支和目标分支的共同祖先之间的差异。因此,它将显示源分支上直到公共祖先的所有更改,而不会考虑目标分支上可能发生的任何更改。

更多信息请访问:https://developer.atlassian.com/blog/2015/01/a-better-pull-request/

基于共同祖先的差异似乎很危险。我希望GitHub能够提供一个更标准的基于3方合并的PR。

编辑-我从来没有提到我总是使用明显的变通办法来避免由此产生的任何问题。只要定期将你的目标分支合并到你的PR分支中,你就不会遇到任何令人不快的意外。

这发生在PRs被压缩和合并选项合并时。如果您希望旧的提交不显示在下面的pr中,那么只需使用创建合并提交选项合并这些pr。

创建合并提交 压缩和合并 重组和合并

注意:一旦完成,下一个pr总是显示他们自己的提交,而不是旧的,因为旧的已经通过创建合并提交选项添加到基础分支。

技术解释:merge -squash和rebase之间的区别是什么?

我找到了一种方法来获得正确的行为(在2020年11月测试)。

git合并和解决冲突后,需要使用git merge——continue而不是git commit ....

这里有一个很好的变通办法。在GitHub中查看PR时,使用Edit按钮将基本分支更改为master以外的内容。然后切换回master,现在它将正确地显示最近提交的更改。