是否可以轻松地将所有提交从一个分支移动到另一个分支作为一个提交?

Is it possible to easily move all commits from one branch onto another as one commit?

我知道,我可以做一个交互式的变基,改写第一个提交并修复所有其他的。但是,如果一个分支包含数百个提交,它就会变得非常乏味。

有没有更简单的方法?

您可以使用 git merge --squash 在合并到分支中时将提交压缩为单个提交。

切换到目标分支

$ git checkout target-branch

然后使用

$ git merge --squash original-branch

original-branch 中的所有提交将合并为一个,并应用于 target-branch

你可以用 git cherry-pick -n 做到这一点。与 git merge --squash 相比,该方法更灵活,因为它允许您指定任意范围的提交:

git cherry-pick -n OTHER_BRANCH~100..OTHER_BRANCH
git commit -m "Merged 100 commits from OTHER_BRANCH"

git cherry-pick

-n|--no-commit

Usually the git cherry-pick command automatically creates a sequence of commits. This flag applies the changes necessary to cherry-pick each named commit to your working tree and the index, without making any commit. In addition, when this option is used, your index does not have to match the HEAD commit. The cherry-pick is done against the beginning state of your index.

This is useful when cherry-picking more than one commits' effect to your index in a row.