Git: "Cannot 'squash' without a previous commit" 变基时出错

Git: "Cannot 'squash' without a previous commit" error while rebase

我在 git rebase -i HEAD~2 的待办事项文本中有以下内容:

pick 56bcce7 Closes #2774
pick e43ceba Lint.py: Replace deprecated link

# Rebase 684f917..e43ceba onto 684f917 (2 command(s))
#
...

现在,当我尝试挤压第一个 (56bcce7) 并通过在第一个之前添加 "s" 来选择第二个时,我收到以下错误:

Cannot 'squash' without a previous commit

谁能解释一下这是什么意思,我该怎么做?

我想压缩第一个提交(56bcce7)和"select and reword"第二个(e43ceba)提交

Interactive rebase 以与您使用 git log 时习惯相反的顺序呈现提交。 git rebase -i 以它们在保存的变基说明文件中列出的确切(自上而下)顺序重播选定的提交。压缩时,为压缩选择的提交与(已编辑)列表中它之前的提交合并,即来自上一行的提交。在您的情况下 - 56bcce7 没有先前的提交。您必须执行以下操作之一

  • git rebase -i HEAD~3(如果你想将 56bcce7 压缩成 684f917
  • 如果您打算将 56bcce7e43ceba 组合,并且 e43ceba 不依赖于 56bcce7,那么只需重新排序即可:

    r e43ceba Lint.py: Replace deprecated link
    s 56bcce7 Closes #2774
    

    UPDATE:下面的提出了一种更好的方法,无需重新排序两个提交:

    r 56bcce7 Closes #2774
    s e43ceba Lint.py: Replace deprecated link
    

    这将 squash/merge 两个提交合二为一。当交互式 rebase 要求 56bcce7 的改写提交消息时,请提供描述 56bcce7e43ceba.

  • 联合的提交消息

我有一个类似的问题,我解决如下:

这是我要压缩的提交组:

1 s 01cc5a08 Removes open div
2 s a2b6eecf Restores old fonts
3 s 603479ff Cleans left out div
4 pick 5afdbc33 Update: show logo on landing page
5 s 04c1cb13 change version of dev and prod from 1 to 2
6 s bbe6a8f8 Update: show logo on landing page if they have one
7 s c0d6008a Adds check for C users

如你所见,我不想。 4,但是 1、2 和 3 之前没有提交 squash into。因此 Cannot 'squash' without a previous commit error.

我的解决方案是对 # r, reword = use commit, but edit the commit message

使用 r 选项

所以我的提交列表是这样的:

1 r 01cc5a08 Removes open div
2 s a2b6eecf Restores old fonts
3 s 603479ff Cleans left out div
4 s 5afdbc33 Update: show logo on landing page
5 s 04c1cb13 change version of dev and prod from 1 to 2
6 s bbe6a8f8 Update: show logo on landing page if they have one
7 s c0d6008a Adds check for C users

保存后,交互式 shell 要求我重新措辞所选提交。

在那之后,我的提交日志产生了一次提交,这导致了更清晰的提交历史。

我刚才也遇到过这个问题,只是 careless.you 可以解决 next:when 你尝试压扁第一个(56bcce7)并选择第二个你应该的问题在第二行之前添加 "s" 但不是第一行。也可以参考下网址:http://backlogtool.com/git-guide/en/stepup/stepup7_5.html

我遇到了这个问题,在我的案例中发生的原因是,你不能将旧的提交压缩到新的提交上。这是一个示例,假设您有 3 个提交:

1 pick 01mn9h78 The lastest commit
2 pick a2b6pcfr A commit before the latest
3 pick 093479uf An old commit i made a while back

现在如果你说 git rebase -i HEAD~3 然后你做类似

1 pick 01mn9h78 The lastest commit
2 s a2b6pcfr A commit before the latest
3 s 093479uf An old commit i made a while back

这将导致错误:

error: cannot 'squash' without a previous commit You can fix this with 'git rebase --edit-todo' and then run 'git rebase --continue'. Or you can abort the rebase with 'git rebase --abort'.

解法:

压缩提交时,您应该将最近的提交压缩到旧的提交而不是相反,因此在示例中它将是这样的:

1 s 01mn9h78 The lastest commit
2 s a2b6pcfr A commit before the latest
3 pick 093479uf An old commit i made a while back

这会很好用,如果你想要所有的提交信息,我会建议 fixup 而不是 squash.

最好在包含提交的交互式编辑器中说,git 总是从下往上压缩,应该在顶部留下一个 "pick" 条目以接收来自的压缩下面。

我刚试过这个方法。

git log -n3

这将显示最后 3 次提交,让我了解什么是最新提交以及之前进行的提交。现在声明变基,

git rebase -i HEAD~3

选择我们需要压缩其他两个的最新提交。选择作为基本提交的提交 ID 就像,

pick commit_id

对于其他两个提交ID,将它们更改为,

squash commit_id

或者简单地说,

s commit_id

用反向逻辑压缩。您将能够在后面的步骤中 select 所需的提交消息。

  • pick 第一个提交 你不想要 的提交消息。
  • squashfixup 您要合并的提交,直到包含您真正想要的提交消息的提交。
pick 56bcce7 Closes #2774
squash e43ceba Lint.py: Replace deprecated link
  • 确认更改 (:x)
  • 删除您不想要的提交消息,只留下您想要提交的消息(在本例中:Lint.py: Replace deprecated link) .
  • 确认选择(:x)

希望有人更清楚✌