我试图合并2个提交为1,所以我遵循“压缩提交与rebase”从git就绪。

我跑

git rebase --interactive HEAD~2

在结果编辑器中,我将pick更改为squash,然后save-quit,但由于出现错误,重基操作失败

没有之前的提交就不能“squash”

现在我的工作树已经达到了这个状态,我很难恢复。

命令git rebase——interactive HEAD~2失败:

交互式改基已经开始

git rebase -continue失败

没有之前的提交就不能“squash”


当前回答

如果你想把几个提交压缩在一起,你可以使用交互式的rebase方法。(谢谢小麦教我这些!)

Git rebase origin/develop -i 然后你只需在你想要压缩的提交前写一个's',让它们卷到主提交中

在git rebase交互模式(vim)下的提示:

导航到要修改的提交行 (ESC) ciw -(改变内部单词)将改变光标下的整个单词。 输入你想做的,例如s表示压扁 (ESC) wq写得挺,你就搞定了。

然后git push -f

其他回答

因为我对几乎所有的东西都使用git,所以对我来说,即使在这里这样做也是很自然的。

假设我签出了branchX,在它的顶端有两个提交,其中我想创建一个合并它们内容的提交,我这样做:

git checkout HEAD^ // Checkout the privious commit
git cherry-pick --no-commit branchX // Cherry pick the content of the second commit
git commit --amend // Create a new commit with their combined content

如果我想更新branchX以及(我认为这是这个方法的缺点),我也必须:

git checkout branchX
git reset --hard <the_new_commit>

如果你想把几个提交压缩在一起,你可以使用交互式的rebase方法。(谢谢小麦教我这些!)

Git rebase origin/develop -i 然后你只需在你想要压缩的提交前写一个's',让它们卷到主提交中

在git rebase交互模式(vim)下的提示:

导航到要修改的提交行 (ESC) ciw -(改变内部单词)将改变光标下的整个单词。 输入你想做的,例如s表示压扁 (ESC) wq写得挺,你就搞定了。

然后git push -f

我经常使用git reset -mixed在你想合并的多次提交之前恢复一个基本版本,然后我做一个新的提交,这样可以让你提交最新的版本,确保你推送到服务器后你的版本是HEAD。

commit ac72a4308ba70cc42aace47509a5e
Author: <me@me.com>
Date:   Tue Jun 11 10:23:07 2013 +0500

    Added algorithms for Cosine-similarity

commit 77df2a40e53136c7a2d58fd847372
Author: <me@me.com>
Date:   Tue Jun 11 13:02:14 2013 -0700

    Set stage for similar objects

commit 249cf9392da197573a17c8426c282
Author: Ralph <ralph@me.com>
Date:   Thu Jun 13 16:44:12 2013 -0700

    Fixed a bug in space world automation

如果我想合并头两个提交为一个,首先我使用:

git reset --mixed 249cf9392da197573a17c8426c282

“249cf9392da197573a17c8426c282”是第三个版本,也是你合并之前的基础版本,在那之后,我做了一个新的提交:

git add .
git commit -m 'some commit message'

希望对每个人来说都是另一种方式。

供参考,从git重置——帮助:

 --mixed
     Resets the index but not the working tree (i.e., the changed files are
     preserved but not marked for commit) and reports what has not been
     updated. This is the default action.

添加到@greg的答案后,你完成了所有的事情,即压缩提交,如果你做git push(原始提交将保留在分支),而如果你做git push -f origin提交将被删除。 例如,你合并了提交B和提交C,如果你使用git push,你将有提交B,提交C和提交BC,但如果你使用git push -f origin,你将只有提交BC

你可以用

git rebase --abort

当你再次运行交互式rebase命令时,'squash;Commit必须在列表中选择Commit的下面