我试图在GitHub上审查一个拉请求到一个不是主的分支。目标分支在master后面,拉请求显示了来自master的提交,所以我合并了master并将其推送到GitHub,但刷新后,他们的提交和差异仍然出现在拉请求中。我已经再次检查了GitHub上的分支是否有来自master的提交。为什么它们仍然出现在拉请求中?
我还检查了本地拉请求,它只显示未合并的提交。
我试图在GitHub上审查一个拉请求到一个不是主的分支。目标分支在master后面,拉请求显示了来自master的提交,所以我合并了master并将其推送到GitHub,但刷新后,他们的提交和差异仍然出现在拉请求中。我已经再次检查了GitHub上的分支是否有来自master的提交。为什么它们仍然出现在拉请求中?
我还检查了本地拉请求,它只显示未合并的提交。
当前回答
我做了什么,为什么会这样?
没有一个解决方案对我有效。当我用两个点,即。。而不是……GH的差异更接近我所改变的。但这还不是我全部的改变。
这是因为GitHub显示压缩合并更改的方式存在问题。最好的解释在这里
它基本上发生在以下情况:
推送featureBranch的变化 挤压合并到主 在本地,当我仍然在featureBranch上时,我将它与main合并。做更多的修改,再把它推上去。 但后来在GitHub上,我看到了比我预期的更多的变化。
值得注意的是,这不是git的问题。相反,这是一个GitHub问题。GitHub无法判断压缩提交与非压缩提交的总和相同,从而导致额外的差异。
解决方案
在本地main上,撤消并保存所有未与main合并的更改。我做到了。例如,如果我有4个提交,不在我的PR得到壁球合并,那么我会这样做:
git重置头~4 Git保存“最近4次提交”
然后用刚才存储的内容创建一个新分支。步骤:
Git checkout main git checkout -b newBranch Git stash应用 Git add—all Git commit -m "some message" git推
其他回答
这发生在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.
编辑:另一种解决方案是重新基底和强制推。干净但被改写的历史
对于遇到这种情况并对GitHub Pull Request行为感到困惑的其他人来说,根本原因是PR是源分支尖端与源分支和目标分支的共同祖先之间的差异。因此,它将显示源分支上直到公共祖先的所有更改,而不会考虑目标分支上可能发生的任何更改。
更多信息请访问:https://developer.atlassian.com/blog/2015/01/a-better-pull-request/
基于共同祖先的差异似乎很危险。我希望GitHub能够提供一个更标准的基于3方合并的PR。
编辑-我从来没有提到我总是使用明显的变通办法来避免由此产生的任何问题。只要定期将你的目标分支合并到你的PR分支中,你就不会遇到任何令人不快的意外。
我不太确定这背后的理论。但我得到了这个几次,并能够通过做下面的事情来解决这个问题。
git pull --rebase
这将从您的原始回购主分支获取并合并更改(如果您有指向)
然后你把你的更改强行推到你的github克隆库(目标)
git push -f origin master
这将确保你的github克隆和你的父repo在相同的github提交级别,你不会看到跨分支的任何不必要的更改。
用2点url代替3点url来比较
而不是
http://githuburl/org/repo/compare/targetbranch...currentbranch
Use
http://githuburl/org/repo/compare/targetbranch..currentbranch
使用git的精选
如果重基过程对您来说像对我一样混乱,另一个选择是使用git精选。以下是步骤:
Update your local target branch using git pull. checkout the branch where you made changes and copy the commit IDs of the commits you want. if the branch name is tangled, do git checkout tangled and then git log. You can scroll through the git log output using the up/down arrows on the keyboard. The commit IDs are the long numbers that each commit contains. Create a new branch from your target branch (e.g main) using git checkout -b new-branch-name when you are on the target branch. On the new branch, do git cherry-pick commit-id where commit-id is the long number that you copied from git log which identifies the commit you want to push. You can do this multiple times, changing the id each time to get another commit. If you run git log on this new branch which you created, you can see that only the changes you have added exist after your target branch's head, as expected. Lastly, push the changes to remote using git push origin new-branch-name and create a pull request.