回滚feature分支合并后,Bitbucket创建Pull Request提示分支已同步
问题描述
我从master分支创建了feature/abc分支,随后通过Git Bash将该分支合并到master,执行步骤如下:
git checkout master git merge feature/abc git push origin master
在Bitbucket上确认分支已合并后,我意识到应该先创建Pull Request并完成代码评审,于是尝试移除已推送到远程master的feature分支合并记录,执行步骤如下:
git checkout master git log # 获取feature/abc的合并提交hash git revert -m 1 <commit-hash> git commit -m "Revert merge of feature/abc" git push origin master
现在Bitbucket上显示了“Revert merge of feature/abc”的提交,master分支中的feature代码已被移除,但尝试从feature/abc向master创建Pull Request时,收到提示:
Branch "master" is already up-to-date with "feature/abc" in repository my-proj.
原因分析
核心原因是master的提交历史中已经存在feature/abc的所有提交记录,git revert操作只是撤销了合并带来的代码变更,并没有从master的提交历史中移除feature/abc的原始提交。Bitbucket的PR检测逻辑是对比两个分支的提交集合,发现feature/abc的所有提交都已经出现在master的历史里,所以提示分支已同步。
解决方法
推荐通过创建新分支的方式来重新发起PR,避免强制推送影响协作:
- 切换到原feature分支并拉取最新代码
git checkout feature/abc git pull origin feature/abc
- 切换到master分支并拉取最新的远程代码
git checkout master git pull origin master
- 基于当前master创建新的feature分支(比如命名为feature/abc-review)
git checkout -b feature/abc-review
- 将原feature/abc分支的提交cherry-pick到新分支:
- 如果原分支只有单个提交,执行:
git cherry-pick <原feature分支的提交hash> - 如果原分支有多个连续提交,执行(替换为起始和结束的提交hash):
git cherry-pick <起始提交hash>..<结束提交hash>
- 如果原分支只有单个提交,执行:
- 处理可能出现的代码冲突,提交后推送到远程
git push origin feature/abc-review
- 现在就可以基于feature/abc-review向master创建Pull Request,完成代码评审后再合并。
如果确定没有其他开发者在使用原feature/abc分支,也可以选择重置原分支到当前master,然后重新提交代码,但这种方式需要强制推送,风险较高:
git checkout feature/abc git reset --hard master # 将原feature分支的代码重新添加并提交 git add . git commit -m "重新提交feature/abc代码用于评审" # 强制推送到远程,注意会覆盖远程分支的历史 git push -f origin feature/abc
内容的提问来源于stack exchange,提问作者likeGreen
相关产品推荐
相关产品推荐

