You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Git合并他人分支到自身分支后内容未显示的解决求助

Git分支合并问题解决方法

问题原因

  • 通过Bitbucket回滚合并请求时,Git会生成一个撤销提交,该提交会抵消his-branch在stable中添加的所有变更。
  • 同步stable到my-branch时,这个撤销提交也被同步过来,导致my-branch中来自his-branch的变更被删除。
  • 再次合并his-branch到my-branch时,Git会根据提交历史判断his-branch的变更已被处理,因此不会重复应用这些变更。

解决方法

方法一:用git cherry-pick逐个应用目标提交

  • 切换到my-branch:
    git checkout my-branch
    
  • 查看his-branch的提交记录,记下需要的提交哈希值:
    git log his-branch
    
  • 逐个将目标提交应用到my-branch:
    git cherry-pick <commit-hash>
    
    • 若遇冲突,解决冲突后执行git cherry-pick --continue;若需跳过该提交,执行git cherry-pick --skip

方法二:创建临时分支重新合并变更

  • 基于my-branch创建临时分支:
    git checkout -b temp-branch my-branch
    
  • 将his-branch合并到临时分支(Git会重新对比文件内容,而非仅依赖提交历史):
    git merge his-branch
    
  • 解决合并冲突后,切换回my-branch并合并临时分支:
    git checkout my-branch
    git merge temp-branch
    
  • 确认无误后删除临时分支:
    git branch -d temp-branch
    

方法三:用git rebase重构提交历史(谨慎使用)

注意:如果his-branch已推送到远程仓库,强制推送会覆盖远程分支历史,需提前告知团队成员。

  • 切换到his-branch:
    git checkout his-branch
    
  • 基于my-branch的最新提交重构his-branch的历史:
    git rebase my-branch
    
  • 切换回my-branch并合并重构后的his-branch:
    git checkout my-branch
    git merge his-branch
    

内容的提问来源于stack exchange,提问作者mrjayviper

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 21:32:51