Rebase只是分支的一部分

时间:2016-01-21 14:20:41

标签: git

我有两个分支(和主人)。分支2基于分支1是基于主。我已经提交了分支1进行审核,它有一些更改,我将其中一些更改重新编入历史记录并将结果合并到主数据中。

现在我需要在master上重新设置分支2以准备审核/合并。

问题是分支2仍然包含分支1的原始提交,它不再存在,因此git会混淆。我尝试使用rebase -i来删除Branch 1的原始提交,但是Branch 2的提交不是基于master-before-branch-1。

我需要做的是采取分支2,删除一些提交,并在单个操作中仅重新设置master上的剩余提交。但我只知道如何在两个不同的步骤中完成这两项操作。

如何将我的分支部分重新绑定到另一个分支上,删除所有不具有共同祖先的提交,除了我指定的那些(例如从HEAD~2起)?

这是当前状态:

master                     new branch 1
- - - - - - - - - - - | - - - - - - - - -
    \
     \   branch 1
      \ _ _ _ _ _ _ _
                     \
                      \     branch 2
                       \ _ _ _ _ _ _ _

我最终希望得到什么:

master            new branch 1    
- - - - - - - | - - - - - - - - - -
                                   \
                                    \
                                     \
                                      \    branch 2
                                       - - - - - - - - - 

5 个答案:

答案 0 :(得分:45)

实际命令是:

git rebase --onto newbranch1 branch1 branch2

这将在<{em> new_branch1之后branch1所有提交之前重播,直至branch2 HEAD。

由于Joshua Goldberg将其in the comments

 git rebase —-onto <place-to-put-it> <last-change-that-should-NOT-move> <change to move>

答案 1 :(得分:20)

解决方案比我预期的要简单得多。事实证明,您可以为更多种类的rebase命令提供-i(我认为这只是为了改变历史记录而将分支重新定位到自身)。所以我只是运行git rebase -i master并删除了那些额外的提交。

答案 2 :(得分:3)

VirtualProtect(thunkMemInfo.BaseAddress, thunkMemInfo.RegionSize, thunkMemInfo.Protect, &oldProtect
  • git rebase --onto master HEAD~2 -您要重新定位到的分支
  • master -您需要重新确定当前分支中的最后n个提交

Source

答案 3 :(得分:2)

我发现您的ASCII图有些含糊,因为分支并不能真正代表一定范围的提交-分支指向特定的提交。我认为您的意思是

H (new-branch-1)
G
| F (HEAD -> branch-2)
| E
| D (branch-1)
| C
|/
B (master)
A

在这种情况下,VonC's answer git rebase --onto new-branch-1 branch-1 branch-2的目标和结果是

F' (HEAD -> branch-2)
E'
H (new-branch-1)
G
| D (branch-1)
| C
|/
B (master)
A

但是your answer git rebase -i master没有任何意义。当然,您的意思是git rebase -i new-branch-1,然后在编辑器中写

drop C
drop D
pick E
pick F

可以实现上述目标。

(您的答案将会产生):

F' (HEAD -> branch-2)
E'
| H (new-branch-1)
| G
|/ 
| D (branch-1)
| C
|/
B (master)
A

答案 4 :(得分:1)

根据回答,您可以使用 --onto 执行此操作。

我发现 git rebase --onto 语法很混乱。所以我创建了这个脚本来重新设置“嵌套”分支:Github Gist

在您的示例中,您将调用:

moveBranch newBranch2 from newBranch1 to master

#!/bin/bash

## Places a branch to a new base. 
## Useful when splitting a long branch to multiple pull requests.
##
##   ---+--------master 
##       \
##         --- A ---- B
## 
## git-moveBranch.sh B from A to master
##
##   ---+-------- master ---- B
##       \
##         --- A 


function printUsageAndExit() {
    echo "Usage: moveBranch <branch> from <previous-base> to <new-base>";
    exit 1;
}

if [ 5 != $# ] ; then printUsageAndExit; fi
if [ "$2" != "from" ] ; then printUsageAndExit; fi
if [ "$4" != "to" ] ; then printUsageAndExit; fi

WHAT="$1"
FROM="$3"
ONTO="$5"

echo "Running:   git rebase —-onto=\"$ONTO\" \"$FROM\" \"$WHAT\""
# git rebase —-onto <place-to-put-it> <last-change-that-should-NOT-move> <change to move>
git rebase --onto "$ONTO" "$FROM" "$WHAT"