首页 > 解决方案 > 如何 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
   \|/
    *    

rebase 的结构是正确的,但相关的分支不会移动。有没有办法移动这些?具体来说,基本技术要求:当为变基处理每个提交时,该提交上存在于远程的任何分支都应移动到新的变基(樱桃采摘)提交。如果这样做,结果将是:

*    [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] 分支可以简单地快进,我们有一个干净的历史记录。有没有办法做到这一点,也许使用管道命令?

标签: gitgit-rebase

解决方案


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

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

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


推荐阅读