撤消Git合并,但保留以后的更改,并重写历史记录

Kar*_*ine 8 git undo git-merge

我有一堆分支,每个分支都有不同的功能.通常我会有一些额外的分支"not_master",它包含master + feature A,如下所示:

(not_master)     a--b--c--d--e---M--x--y--z
(feature A)           --h--i--j-/
Run Code Online (Sandbox Code Playgroud)

有时我想取消合并功能A,但保持x,y,z"not_master" 提交.

换句话说,我想这样:

(not_master)     a--b--c--d--e--x--y--z
Run Code Online (Sandbox Code Playgroud)

我看到我可以做一个git revert -m 1 M会在最终添加提交以恢复我的更改,但我真的不想这样做,因为这些提交还没有发布,所以添加更多提交会使历史记录更难阅读.

其他人建议只做一个git reset --hard M,但这将倾倒变化x,y,z.我只是在想这个完全错误的方式吗?我应该git reset --hard M和樱桃挑选x,y,z吗?

wno*_*ise 8

git rebase会做你想做的. git rebase -i <commit e>应该工作:它将打开一个编辑器,你删除合并提交并保存.

你也应该能够直接在e上指定rebasing x..z,但是整理出命令行的语义有点毛茸茸.如果我正在阅读手册,那应该是git rebase --onto <commit e> <commit M> not_master.

编辑:测试并似乎工作:

mkdir foo; cd foo
git init
touch initial
git add initial
git commit -m 'initial'
git checkout -b topic
touch topic
git add topic
git commit -m 'topic'
git checkout master
touch master
git add master
git commit -m 'master'
git tag pre_merge
git merge topic
git tag merge
touch post_topic
git add post_topic
git commit -m 'post topic'
git rebase --onto pre_merge merge master
Run Code Online (Sandbox Code Playgroud)

结果有一段历史

initial -- master -- post topic  (master)
      \
       \-- topic                 (topic)
Run Code Online (Sandbox Code Playgroud)

  • 呃,这太烦人了.尝试我添加的备用命令.(它可能还需要`--force-rebase`). (2认同)