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

我跑

git rebase --interactive HEAD~2

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

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

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

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

交互式改基已经开始

git rebase -continue失败

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


当前回答

$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

其他回答

我经常使用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 rebase -i将两个提交压缩成一个。

如果你只想合并两个提交,并且它们是“最近的两个”,可以使用以下命令将两个提交合并为一个:

git reset --soft "HEAD^"
git commit --amend

因为我对几乎所有的东西都使用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>

如果你的主分支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 rebase --abort

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