我用:

git checkout -b testbranch

我做了20次提交。

现在我想要压缩这20个提交。我是这样做的:

git rebase -i HEAD~20

如果我不知道有多少次提交呢?有没有什么方法可以做到:

git rebase -i all on this branch

当前回答

你正在做的事情很容易出错。只做:

git rebase -i master

它会自动将你的分支的提交重置到当前最新的主节点上。

其他回答

你正在做的事情很容易出错。只做:

git rebase -i master

它会自动将你的分支的提交重置到当前最新的主节点上。

签出您希望将所有提交压缩为一次提交的分支。我们说它叫feature_branch。

git checkout feature_branch

步骤1:

用你的本地主分支对你的origin/feature_branch进行软重置(根据你的需要,你也可以用origin/main重置)。这将重置feature_branch中所有额外的提交,但不会在本地更改任何文件更改。

git reset --soft main

步骤2:

将git repo目录中的所有更改添加到将要创建的新提交中。并通过信息提交相同的信息。

# Add files for the commit.
git add ...
git commit -m "commit message goes here"

在之前的回答中,我没有看到任何关于如何处理“凌乱的分支”和“自我冲突”的信息。例如,我经常在我的特性分支(称为特性)上有主提交,这导致了它们之间的冲突。我发现这是最难处理的问题之一。

如何挤压凌乱的树枝?使用临时分支!

我发现Felix Rieseberg的解决方案是最好的。以下是我对他的建议略短的抄写:

Create a local tmp branch of off master git checkout master && git pull && git checkout -b tmp Merge all feature changes into tmp (without any commits, only staged file changes). git merge --squash $feature Manually solve all remaining "real conflicts" (This is the only step you cannot have a script do for you) Commit. tmp is now master + 1 commit (containing all changes). git commit ... Checkout feature and git reset --hard tmp (feature's original contents are gone, and it is now basically tmp, but renamed) git checkout $feature && git reset --hard tmp Ignore and override origin/feature (then clean up) git push -f && git branch -D tmp

Felix指出,这将产生最干净的合并,没有任何来自master和feature之间混乱/复杂关系的奇怪的自我冲突:

您可能会遇到一些不可避免的合并冲突。请相信,这是尽可能少的冲突,因为您跳过了最初创建的许多中间提交。

另一种解决方案是将所有提交日志保存到一个文件中

分支> git 日志.log

现在branch.log将拥有自开始以来的所有提交id。向下滚动并进行第一次提交(这在终端中很困难) 使用第一次提交

Git复位-软

所有提交都将被压缩

为了完善一下Caveman的回答,使用git reset——soft <commit>。从文档中,这个命令:

根本不触及索引文件或工作树(但将头部重置为<commit>,就像所有模式一样)。这将使所有更改过的文件都变成“要提交的更改”,就像git状态所显示的那样。

换句话说,它将撤销到<commit>之前的所有提交。但是它不会改变工作目录。您最终会得到所有的更改,这些更改都是未分期和未提交的。就好像那些介入的提交从未发生过一样。

例子:

# on master
git checkout -b testbranch
# make many commits
git reset --soft master
git add .
git commit -m 'The only commit.'

此时,您仍然在testbranch上,它只有一次提交。像往常一样合并到master中。

在我的手中,Caveman回答的第一部分(git rebase -i)并没有压缩提交。