首页 > 解决方案 > Git Rebase 已合并分支

问题描述

我从主develop分支创建了功能分支,称为my-feature另一个开发人员创建的另一个功能分支another-feature

我们都在不同的文件中进行更改

另一个开发人员已经创建了 PR,但尚未合并到开发中。但是,我需要他对我的分支进行更改,所以我git merge another-feature我的功能分支进行了更改并创建了拉取请求。

但是,主要问题是现在,我可以看到another-feature分支对我的拉取请求的更改my-feature。那么,我该如何重新设置呢?我不想显示来自另一个分支的更改。

我检查了另一篇文章,但其中一些对我来说不太容易理解。

任何帮助将不胜感激。

编辑:

git log显示如下。(目标分支是develop

commit: f0fdfddsdsde
Author: Me (As my last commit before creating PR)
Date: blah blah

commit message....


commit: a4dsdsdssdds
Author: Another developer (whose changes I merged)
Date: blah blah

commit message....

rest of another commits ....

git log ––all ––decorate ––oneline ––graph

f0acaed (HEAD -> feature/my-feature, origin/my-feature) commit message..
a4f4dab (origin/another-feature, feature/another-feature) commit message..
* db30503 (origin/HEAD, origin/develop, develop) merge pull request from another branch

blah blah blah

标签: gitmergegit-mergerebasegit-rebase

解决方案


another-feature除非合并,否则更改将在您的 PR 上可见。
如果您只是不想在审查时看到 PR 中的这些更改,只需在审查期间更改基本分支(如果您使用的版本控制系统允许)。

另一个解决方案(我更喜欢)是重新设置您的分支,another-feature而不是合并它们。
在这种情况下,当另一个特性被合并时,你就不会那么头疼了,而且在审查时,你将能够只审查你推送的提交(大多数系统允许你在审查时选择提交集)。

更新:( 对于上面提到的第二种方法)
为了跳转到 的原始状态my-feature,您需要使用reflog.
这是如何做到这一点的。

  1. 通过键入确保没有待处理的更改 git status
  2. 键入 git reflog,您会看到类似这样的内容: 在此处输入图像描述 您可以在此处看到头部引用的历史记录。只需找到合并之前的提交(在图片中它是HEAD@{1}带有 commit hash 的提交e919ec6)。
  3. 现在只需使用以下命令签出该提交:( git checkout HASH_HERE例如git checkout e919ec6
  4. 现在只需从该提交创建一个分支(为方便起见) checkout -b my-feature.ORIGINAL

现在,my-feature.ORIGINAL在合并之前包含您的分支状态,并且您在该分支上。
为了进行变基而不是合并,请按照下列步骤操作:

  1. 备份您的分支(再次只是为了方便) git branch my-feature.BKP
  2. 使用另一个功能重新设置分支 git rebase another-feature
  3. 重命名旧的坏分支(为方便起见) git rename my-feature my-feature.BAD
  4. 将您当前的分支名称从 my-feature.ORIGINAL 更改为 my-feature (以匹配远程分支名称) git rename my-feature.ORIGINAL my-feature
  5. 现在您可以强制将您的分支推送到您的远程分支,并且 PR 将被更新。

推荐阅读