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


当前回答

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

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

此分類上一篇

其他回答

使用 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 命令使用。

 git rebase -i HEAD~n

n(=4在这里)是最后的承诺的数字,然后你得到了以下选项,

pick 01d1124 Message....
pick 6340aaa Message....
pick ebfd367 Message....
pick 30e0ccb Message....

更新如下,选择一个承诺,并将其他人推到最新的,

p 01d1124 Message....
s 6340aaa Message....
s ebfd367 Message....
s 30e0ccb Message....

详细信息点击链接

您可以使用 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 &&吉特承诺是,你会得到承诺的消息,以每一个承诺的消息,你正在破坏。

基于这篇文章,我发现这个方法对我的使用更容易。

我的“dev”分支在“起源/dev”前面有96个承诺(因此这些承诺尚未推到远程)。

我宁愿将分支重新设置为“起源/dev”状态(这将使96个分支的所有变更无效),然后立即进行变更:

git reset origin/dev
git add --all
git commit -m 'my commit message'

如果您不关心承诺信息的承诺之间,您可以使用

git reset --mixed <commit-hash-into-which-you-want-to-squash>
git commit -a --amend