这句话的语境是:

master在X quickfix1在X + 2次提交时

这样:

o-o-X (master HEAD)
     \
      q1a--q1b (quickfix1 HEAD)

然后我开始开发quickfix2,但意外地把quickfix1作为要复制的源分支,而不是主分支。现在quickfix2是X + 2个提交+ 2个相关提交。

o-o-X (master HEAD)
     \
      q1a--q1b (quickfix1 HEAD)
              \
               q2a--q2b (quickfix2 HEAD)

现在我想要一个带有quickfix2的分支,但不包含属于quickfix1的2个提交。

      q2a'--q2b' (quickfix2 HEAD)
     /
o-o-X (master HEAD)
     \ 
      q1a--q1b (quickfix1 HEAD)

我尝试从quickfix2中的某个修订中创建一个补丁,但补丁不保存提交历史。是否有一种方法可以保存我的提交历史,但在quickfix1中有一个没有更改的分支?


你可以使用git cherry-pick来选择你想要复制的提交。

可能最好的方法是在master上创建分支,然后在该分支中使用git从quickfix2中选择你想要的2个提交。

这是一个典型的rebase -onto案例:

 # let's go to current master (X, where quickfix2 should begin)
 git checkout master

 # replay every commit *after* quickfix1 up to quickfix2 HEAD.
 git rebase --onto master quickfix1 quickfix2 

所以你应该从

o-o-X (master HEAD)
     \ 
      q1a--q1b (quickfix1 HEAD)
              \
               q2a--q2b (quickfix2 HEAD)

to:

      q2a'--q2b' (new quickfix2 HEAD)
     /
o-o-X (master HEAD)
     \ 
      q1a--q1b (quickfix1 HEAD)

这最好在一个干净的工作树上完成。 参见git配置——global rebase。autostash为true,尤其是在Git 2.10之后。

我认为是:

git checkout master
git checkout -b good_quickfix2
git cherry-pick quickfix2^
git cherry-pick quickfix2

你能做的最简单的事情就是挑选一个范围。它的作用与rebase相同,但对眼睛更容易:)

git cherry-pick quickfix1..quickfix2
// on your branch that holds the commit you want to pass
$ git log
// copy the commit hash found
$ git checkout [branch that will copy the commit]
$ git reset --hard [hash of the commit you want to copy from the other branch]
// remove the [brackets]

其他更有用的命令在这里有解释:Git指南