我如何将最后的N承诺集成到一个承诺中?


当前回答

例如,如果您想在一个分支(远程存储库)中排除最后3个承诺,例如: https://bitbucket.org

我所做的就是

git reset --soft HEAD~3 git commit git push 起源 <branch_name> --force

其他回答

要将最后10个承诺分为1个单一承诺:

git reset --soft HEAD~10 && git commit -m "squashed commit"

如果你也想更新远程分支与破碎的承诺:

git push -f

除了其他精彩的答案之外,我还想补充一下,Git Rebase -i 如何总是让我与承诺命令混淆 - 老到新或相反? 所以这就是我的工作流:

git rebase -i HEAD~[N], N 是我想加入的命令的数量,从最新的命令开始。 所以 git rebase -i HEAD~5 意味着“将最后 5 命令分成一个新的命令”; 编辑打开,显示我想合并的命令列表。

来源和其他阅读: #1, #2。

Anomies 答案很好,但我对此感到不安全,所以我决定添加几张屏幕截图。

步骤 0: git log

查看你在哪里与 git log. 最重要的是,找到第一个 commit 的 commit hash 你不想 squash。

此分類上一篇

步骤一:转动

在我的案例中,执行 git rebase -i(你的哈希):

$ git rebase -i 2d23ea524936e612fae1ac63c95b705db44d937d

步骤2:选择 / 切割你想要的东西

在我的情况下,我想把所有的承诺,这是第一次在时间上。 订单是从第一到最后,所以正如在 git log。 在我的情况下,我想:

此分類上一篇

步骤 3: 调整消息(s)

如果您只选出一个承诺,并解除其余的承诺,您可以调整一个承诺消息:

此分類上一篇

一旦你保存这个(:wq),你已经完成了。

您可以使用 git merge --squash 为此,这比 git rebase -i 略有优雅。

警告: 首先,请确保您完成工作 - 检查 git 状态是干净的(因为 git 重新设置 - 硬将抛弃错误和错误的变化)

然后:

# Reset the current branch to the commit just before the last 12:
git reset --hard HEAD~12

# HEAD@{1} is where the branch was just before the previous command.
# This command sets the state of the index to be as it would just
# after a merge from that commit:
git merge --squash HEAD@{1}

# Commit those squashed changes.  The commit message will be helpfully
# prepopulated with the commit messages of all the squashed commits:
git commit

git merge 的文档详细描述了 --squash 选项。


更新:这个方法的唯一真正优点,比克里斯·约翰森(Chris Johnsen)在他的回答中提出的更简单的吉特重新设置 --soft HEAD~12 &&吉特承诺是,你会得到承诺的消息,以每一个承诺的消息,你正在破坏。

在分支中,你想将承诺结合起来,运行:

git rebase -i HEAD~(n number of commits back to review)

例子:

git rebase -i HEAD~2

此将打开文本编辑器,您必须在每个承诺前更换“点击”,如果您希望这些承诺合并。

p, pick = 使用 commit

s, squash = 使用承诺,但输入前承诺

例如,如果您正在寻找将所有承诺合并到一个,则“选择”是您所做的第一个承诺,并且所有未来的承诺(位于第一个下方)都应该设置为“滑动”。如果使用vim,请使用 :x 在输入模式中保存和输出编辑器。

然后继续下调:

git add .

git rebase --continue

更多关于此和其他方式重新写下你的承诺历史,请参见这篇有用的文章