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


当前回答

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

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

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

git push -f

其他回答

git rebase -i HEAD^^

在哪里 ^'s 的数量是 X

(此分類上一篇: 最後兩個命令)

使用 git rebase -i <after-this-commit> 并在第二次和随后的命令中取代“squash”或“fixup”,如手册中所描述。

在此例子中, <after-this-commit> 是 SHA1 hash 或当前分支的 HEAD 的相对位置,从该分支的 Commits 被分析为 rebase 命令. 例如,如果用户希望从当前 HEAD 查看 5 个 Commits 在过去的命令是 git rebase -i HEAD~5.

您可以使用 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 reset --mixed <commit-hash-into-which-you-want-to-squash>
git commit -a --amend

要避免在重定向同一分支中的承诺时不得不解决任何合并冲突,您可以使用下列命令:

git rebase -i <last commit id before your changes start> -s recursive -X 我們

要将所有承诺分成一个,当您被要求编辑将合并的承诺时(在旗帜上),更新所有但第一个行动,从选择到分解,如其他答案中所建议。

在这里,我们使用合并策略(旗帜)回归和战略选项(X)我们的,以确保历史上后来的承诺赢得任何合并冲突。

注意:不要把这与我们做别的事情的 git rebase 混淆。

引用: git rebase recursive merge 策略