我有一段历史,看起来有点像以下
* (TOPIC) topic 3
|
| * merge master onto topic
|/|
* | master 3
| * topic 2
* | master 2
| |
| * topic 1
|/
* master 1
|
Run Code Online (Sandbox Code Playgroud)
即上游分支合并到主题一次。现在我想在同一个基础上以交互方式重新设置这个主题分支,但撤消合并(合并提交本身和合并引入的 master 的所有提交)。这怎么可能?
让我们为绘图中描述的提交使用有效标识符:
* topic3 (TOPIC) topic 3
|
| * topic2merge merge master onto topic
|/|
* | master3
| * topic2
* | master2
| |
| * topic1
|/
* master1
|
Run Code Online (Sandbox Code Playgroud)
如果要删除合并提交(topic2在 之后成为第一次提交的唯一父项merge master onto topic),则需要运行以下git rebase命令:
git rebase --onto topic2 topic2merge topic3
Run Code Online (Sandbox Code Playgroud)
如果当前分支topic3已经存在,你可以topic3从上面的命令中省略(如果它存在,首先git rebase检查它,如果它已经不是当前分支)。
执行此操作后,图形如下所示:
| * topic3 <-- (TOPIC) topic 3
| |
* | master3
| * topic2
* | master2
| |
| * topic1
|/
* master1
|
Run Code Online (Sandbox Code Playgroud)
包含topic2merge和之间的提交topic3仍然存在于存储库中,但它们不再可见。如果它们是可访问的,它们仍然可见,如果有指向它们的分支,就会发生这种情况。甚至远程分支。
如果您已经推topic3送到远程仓库,您必须运行git push -f origin topic3(替换origin为您的远程名称)并通知您的同事您已更改历史记录的事实。他们需要知道这一点;在他们的存储库中topic3仍然有topic2merge其历史记录,您所做的更改代表了他们的存储库不会自动采用的替代历史记录。他们必须使用git reset --hard或git rebase或git cherry-pick跟上。
您可以使用命令
git rebase -i --onto master1 master topic
Run Code Online (Sandbox Code Playgroud)
这将获取所有在topic但不在master(主题1、主题2、主题3)中的非合并提交,并将它们应用到master1提交之上。