如何git rebase,保留合并和移动所有分支?

时间:2019-07-12 16:56:12

标签: git git-rebase

我们在多个团队中使用git进行版本控制。每个团队都将功能交付到团队分支(合并)中,如下所示:

*    [integration_team1]  Merge team1_featureB into integration_team1
|\
| *  [team1_featureB]     Comment about feature B code
|/
*                         Merge team1_featureA into integration_team1
|\
| *  [team1_featureA]     Comment about feature A code
|/
*    [release]

(Names in square brackets are branch names.)

我们会定期将集成分支交付到[release]中,以保留历史记录。在一个团队中,这很容易:将[integration_team1]合并到[release]中,快速转发。但是,当涉及两个或更多团队时,该图可能看起来像这样:

*        [integration_team1][release] Merge team1_featureB into integration_team1
|\
| *      [team1_featureB]     Comment about feature B code
|/
*                             Merge team1_featureA into integration_team1
|\
| *      [team1_featureA]     Comment about feature A code
| |
| | *    [integration_team2]  Merge team2_featureD into integration_team2
| | |\
| | | *  [team2_featureD]     Comment about feature D code
| | |/
\ | *                         Merge team2_featureC into integration_team2
 \| |\
  \ | *  [team2_featureC]     Comment about feature C code
   \|/
    *    

我想将[integration_team2]改写到[release],保留合并并移动所有分支。如果我这样做:

git checkout integration_team2
git rebase --rebase-merges release

然后我得到这张图:

*    [integration_team2]  Merge team2_featureD into integration_team2
|\
| *                       Comment about feature D code
|/
*                         Merge team2_featureC into integration_team2
|\
| *                       Comment about feature C code
|/
*    [integration_team1][release] Merge team1_featureB into integration_team1
|\
| *  [team1_featureB]     Comment about feature B code
|/
*                         Merge team1_featureA into integration_team1
|\
| *  [team1_featureA]     Comment about feature A code
| |
| | *    [team2_featureD] Comment about feature D code
| | |
\ | *                     Merge team2_featureC into integration_team2
 \| |\
  \ | *  [team2_featureC] Comment about feature C code
   \|/
    *    

重组的结构是正确的,但是相关的分支不会移动。有办法移动那些吗?具体来说,这是基本的技术要求:处理每个提交以进行基准更改时,远程上存在的该提交上的任何分支都应移至新的重新确定的(樱桃挑选的)提交中。如果这样做,结果将是:

*    [integration_team2]  Merge team2_featureD into integration_team2
|\
| *  [team2_featureD]     Comment about feature D code
|/
*                         Merge team2_featureC into integration_team2
|\
| *  [team2_featureC]     Comment about feature C code
|/
*    [integration_team1][release] Merge team1_featureB into integration_team1
|\
| *  [team1_featureB]     Comment about feature B code
|/
*                         Merge team1_featureA into integration_team1
|\
| *  [team1_featureA]     Comment about feature A code
|/
* 

从这一点来看,[release]分支可以简单地快速转发,并且我们拥有清晰的历史记录。 有什么方法可以做到这一点,也许可以使用管道命令?

2 个答案:

答案 0 :(得分:0)

git rebase仅在当前分支上运行。我在这里想到的唯一解决方案是分别重置每个分支:

git checkout <branch>
git reset --hard <hash>

您可以从git log中获取哈希值,并以与原始提交相同的消息查找重新建立的提交。

答案 1 :(得分:-1)

  

我想将[integration_team2]建立在[release]的基础上,保留合并并移动所有分支。

您要执行的重新配置将重写所有[integration_team2]提交,将[release]中当前的所有更改合并到它们的每个中,就好像团队2最初是基于当前的[版本],而不是原始版本。

这会使所有基于重新提交的提交的所有测试无效。如果您想要Git历史记录的通用规则,那么“已发布的提交一定不能破坏构建”将必须放在榜首。重新设置基准后,您必须返回并重新测试其中的每一个。

如果您要合并两个经过独立开发和测试的已发布分支,请合并它们。这就是这里真正发生的事情,您正在合并分支机构。如果您的历史是谎言,这对任何人都没有帮助。 ..._ team2分支不是基于..._ team1的工作开发的,也没有基于...的测试,因此,您正在合并完整的功能集。