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


当前回答

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

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

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

git reset origin/dev
git add --all
git commit -m 'my commit 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 &&吉特承诺是,你会得到承诺的消息,以每一个承诺的消息,你正在破坏。

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

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

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

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

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

引用: git rebase recursive merge 策略

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

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

如果您使用 TortoiseGit,您可以将函数结合到一个承诺:

打开 TortoiseGit 背景菜单 选择显示日志 标记日志视图中的相关任务 从背景菜单中选择组合到一个任务

此分類上一篇

此功能自动执行所有必要的单 git 步骤 不幸的是,仅适用于 Windows。