如何将提交重播到已经包含提交的分支上?
How to replay a commit onto a branch that already contains the commit?
假设我们的提交历史看起来像
1--2--3--4
^
HEAD
其中 4 是最近的提交。
有没有办法将提交 2 的更改(2 和 1 之间的差异)重播到提交 4?
您可能想知道为什么有人会想要这样做。假设这是您的生产分支,它应该始终处于工作状态。比方说,当提交历史看起来像
1--2
^
HEAD
您意识到并认为提交 2 可能会破坏所有内容,因此您很快推出了一个恢复提交,其中提交 3 恢复了提交 2。然后有人提交了包含您想要保留的好内容的提交 4。此时你意识到提交 2 实际上没问题,所以你想在 4 之上重播它。
git cherry-pick A
可以做到。
见http://git-scm.com/docs/git-cherry-pick
给定一个或多个现有提交,应用每个提交引入的更改,并为每个记录一个新的提交。这需要您的工作树是干净的(没有来自 HEAD 提交的修改)。
例如git cherry-pick master~4 master~3
您想要什么是恢复提交 3 和 4 中引入的更改?如果是这样,请尝试:
$ git diff commit2..commit4 | git apply -R
这仅在您要还原的提交之间的历史是线性的情况下才有效。
You are probably wondering why anyone would want to do that. Lets say that this is your production branch that is always supposed to be in a working state. Lets say that earlier, when the commit history looked like
1--2
^
HEAD
You had a realization and thought that commit 2 might break everything, and so you quickly pushed out a revert commit, where commit 3 reverts commit 2. Then someone makes commit 4 that contains good content you want to keep. At this point you realize that commit 2 was actually ok, and so you want to replay it on top of 4.
然后你想恢复提交 3(恢复提交 2),因此 恢复 提交 2:
git revert 3
假设我们的提交历史看起来像
1--2--3--4 ^ HEAD
其中 4 是最近的提交。
有没有办法将提交 2 的更改(2 和 1 之间的差异)重播到提交 4?
您可能想知道为什么有人会想要这样做。假设这是您的生产分支,它应该始终处于工作状态。比方说,当提交历史看起来像
1--2 ^ HEAD
您意识到并认为提交 2 可能会破坏所有内容,因此您很快推出了一个恢复提交,其中提交 3 恢复了提交 2。然后有人提交了包含您想要保留的好内容的提交 4。此时你意识到提交 2 实际上没问题,所以你想在 4 之上重播它。
git cherry-pick A
可以做到。
见http://git-scm.com/docs/git-cherry-pick
给定一个或多个现有提交,应用每个提交引入的更改,并为每个记录一个新的提交。这需要您的工作树是干净的(没有来自 HEAD 提交的修改)。
例如git cherry-pick master~4 master~3
您想要什么是恢复提交 3 和 4 中引入的更改?如果是这样,请尝试:
$ git diff commit2..commit4 | git apply -R
这仅在您要还原的提交之间的历史是线性的情况下才有效。
You are probably wondering why anyone would want to do that. Lets say that this is your production branch that is always supposed to be in a working state. Lets say that earlier, when the commit history looked like
1--2 ^ HEAD
You had a realization and thought that commit 2 might break everything, and so you quickly pushed out a revert commit, where commit 3 reverts commit 2. Then someone makes commit 4 that contains good content you want to keep. At this point you realize that commit 2 was actually ok, and so you want to replay it on top of 4.
然后你想恢复提交 3(恢复提交 2),因此 恢复 提交 2:
git revert 3