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


当前回答

您可以使用 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

如果您正在使用 GitUp,选择您想要与其父母合并的承诺,并按下 S. 您必须为每个承诺做一次,但它比与正确的命令线引发更简单。

2020年简单的解决方案:

git reset --soft HEAD~2 
git commit -m "new commit message"
git push -f

2 意味着最后两个命令将被打破,你可以用任何数字取代它。

如果您想将每个承诺分成一个单一承诺(例如第一次公开发布项目时),请尝试:

git checkout --orphan <new-branch>
git commit

如果你在一个遥远的分支(所谓的功能分支)从一个黄金储备(黄金_repo_name),那么这里是技巧将你的承诺分成一个:

Checkout the golden repo git checkout golden_repo_name 创建一个新的分支从它(黄金 repo)如下 git checkout -b dev-branch Squash merge 与您的本地分支 你已经 git merge --squash 功能分支 承诺您的变化(这将是唯一的承诺在 dev-branch) git commit -m "My feature complete" 将分支推到您的本地存储库 git push 起源 d