9

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

(not_master)     a--b--c--d--e---M--x--y--z
(feature A)           --h--i--j-/

有时我想取消合并功能 A,但将提交保留x,y,z在“not_master”中。

换句话说,我想要这样:

(not_master)     a--b--c--d--e--x--y--z

我看到我可以做一个git revert -m 1 M将在末尾添加一个提交以恢复我的更改,但我真的不想这样做,因为这些提交尚未发布,所以添加更多提交会使历史更难阅读.

其他人建议只做 a git reset --hard M,但这会转储x,y,z. 我只是在想这个完全错误的方式吗?我应该只git reset --hard M挑选x,y,z吗?

4

1 回答 1

9

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

您还应该能够直接指定将 x..z 重新定位到 e 上,但是整理命令行的语义有点麻烦。如果我正确阅读手册页,它应该是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

导致历史

initial -- master -- post topic  (master)
      \
       \-- topic                 (topic)
于 2011-01-25T20:21:33.880 回答