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


当前回答

有人提到在 IntelliJ IDEA UI 上做得多容易:

转到 git 窗口 手动选择所有要融入一个的承诺. 右键单击 > Squash 承诺 > 编辑失败的承诺消息 点击左侧的分支名称 > 右键单击 > Push > Force Push

此分類上一篇

其他回答

这是超级可爱的,但在某种寒冷的方式,所以我只会把它推到戒指中:

GIT_EDITOR='f() { if [ "$(basename $1)" = "git-rebase-todo" ]; then sed -i "2,\$s/pick/squash/" $1; else vim $1; fi }; f' git rebase -i foo~5 foo

翻译:为 git 提供一个新的“编辑器”,如果要编辑的字体名称是 git-rebase-todo(互动的 rebase prompt) 会改变所有,但第一个“点”到“squash”,否则 spawns vim - 这样,当你被要求编辑破碎的承诺消息时,你会得到 vim。

也许我会做马克·隆加尔所建议的事情。

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

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

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

git push -f

您可以使用 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 如何总是让我与承诺命令混淆 - 老到新或相反? 所以这就是我的工作流:

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

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

git rebase -i HEAD^^

在哪里 ^'s 的数量是 X

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