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

我跑

git rebase --interactive HEAD~2

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

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

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

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

交互式改基已经开始

git rebase -continue失败

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


当前回答

你可以用

git rebase --abort

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

其他回答

我经常使用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.

如果你的主分支git日志看起来像下面这样:

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 checkout 77df2a40e53136c7a2d58fd847372 -b合并提交 现在,只需要从上次提交到这个新分支的更改中选择:git cherry-pick -n -x ac72a4308ba70cc42aace47509a5e。(如有冲突,应及时解决) 你在上次提交中的改变在第二次提交中。但是你仍然需要提交,所以首先添加你刚刚选择的更改,然后执行git commit—amend。

就是这样。如果你愿意,你可以在分支“merge -commits”中推送这个合并的版本。

此外,您现在可以在主分支中丢弃连续两次提交。只需将你的主分支更新为:

git checkout master
git reset --hard origin/master (CAUTION: This command will remove any local changes to your master branch)
git pull

$git变基--堕胎

如果你想要撤销git rebase,可以在任何时候运行这段代码

我们赢了

重新应用最近两次提交。上面的命令将打开一个代码编辑器

[最近一次提交将在底部]。改变最后一个 致力于壁球运动。因为squash将与之前的提交融合。 然后按esc键并输入:wq保存退出

输入:wq后,您将进入active rebase模式

注意:如果没有警告/错误消息,您将获得另一个编辑器,如果有错误或警告另一个编辑器将不显示,您可以通过运行中止 $ git rebase -中止如果你看到一个错误或警告,否则继续运行$ git rebase -继续

您将看到2 commit消息。选择一个或自己写提交信息,保存并退出[:wq]

注意2:如果运行rebase命令,可能需要强制将更改推送到远程回购

$ git push -f

$ git push -f origin master

如果您想合并两个最近的提交,而只使用旧的提交的消息,您可以使用expect自动化该过程。

我认为:

您使用vi作为编辑器 每次提交只有一行

我用git版本2.14.3 (Apple git -98)进行了测试。


#!/usr/bin/env expect
spawn git rebase -i HEAD~2

# change the second "pick" to "squash"
# down, delete word, insert 's' (for squash), Escape, save and quit
send "jdwis \033:wq\r"

expect "# This is a"

# skip past first commit message (assumed to be one line), delete rest of file
# down 4, delete remaining lines, save and quit
send "4jdG\r:wq\r"

interact

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