Git:变基时“无法在没有先前提交的情况下‘挤压’”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39595034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git: "Cannot 'squash' without a previous commit" error while rebase
提问by Dawny33
I have the following in the to-do text of git rebase -i HEAD~2
:
我的待办事项文本中有以下内容git rebase -i HEAD~2
:
pick 56bcce7 Closes #2774
pick e43ceba Lint.py: Replace deprecated link
# Rebase 684f917..e43ceba onto 684f917 (2 command(s))
#
...
Now, when I try to squash the first one(56bcce7
) and pick the second one by adding "s" before the first, I get the following error:
现在,当我尝试压缩第一个 ( 56bcce7
) 并通过在第一个之前添加“s”来选择第二个时,出现以下错误:
Cannot 'squash' without a previous commit
Can someone explain me what it means, and how do I do it?
有人可以解释一下这是什么意思,我该怎么做?
I want to squash the first commit(56bcce7
) and "select and reword" the second(e43ceba
) commit
我想压缩第一个提交(56bcce7
)并“选择并改写”第二个(e43ceba
)提交
采纳答案by Leon
Interactive rebase presents commits in the reverse order of what you are used to when using git log
. git rebase -i
replays the selected commits in the exact (top-down) order they are listed in the saved rebase instructions file. When squashing, the commit selected for squashing is combined with the commit that precedes it in the (edited) list, i.e. the commit from the previous line. In your case - there is no previous commit for 56bcce7
. You have to do one of the following
交互式 rebase 以与您在使用git log
. git rebase -i
以它们在保存的 rebase 指令文件中列出的确切(自上而下)顺序重放所选提交。压缩时,为压缩选择的提交与(已编辑)列表中位于它之前的提交相结合,即来自前一行的提交。在您的情况下 - 之前没有提交 56bcce7
. 您必须执行以下操作之一
git rebase -i HEAD~3
(if you want to squash56bcce7
into684f917
)If you mean to combine
56bcce7
withe43ceba
, ande43ceba
doesn't depend on56bcce7
, then simply reorder them:r e43ceba Lint.py: Replace deprecated link s 56bcce7 Closes #2774
UPDATE: Gus's answerbelow suggests a better way of doing the same, without reordering the two commits:
r 56bcce7 Closes #2774 s e43ceba Lint.py: Replace deprecated link
This will squash/merge the two commits into one. When the interactive rebase asks for a reworded commit message for
56bcce7
, provide the commit message that describes the union of56bcce7
ande43ceba
.
git rebase -i HEAD~3
(如果你想挤进56bcce7
去684f917
)如果你的意思是结合
56bcce7
与e43ceba
和e43ceba
不依赖于56bcce7
,然后简单地重新排序:r e43ceba Lint.py: Replace deprecated link s 56bcce7 Closes #2774
更新:Gus 在下面的回答提出了一种更好的方法来做同样的事情,而无需重新排序两次提交:
r 56bcce7 Closes #2774 s e43ceba Lint.py: Replace deprecated link
这会将两个提交压缩/合并为一个。当交互式 rebase 要求为 重新编写提交消息时
56bcce7
,请提供描述56bcce7
和 并集的提交消息e43ceba
。
回答by Gus
I had a similar problem which I solved as follows:
我有一个类似的问题,我解决如下:
This is the commit group I wanted to squash:
这是我想压制的提交组:
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
As you can see, I wanted no. 4, but 1, 2 and 3 had no previous commit to squash into. Hence the Cannot 'squash' without a previous commiterror.
正如你所看到的,我不想。4,但是 1、2 和 3 之前没有提交压缩到. 因此,不能在没有先前提交错误的情况下“挤压”。
My solution was to use the r
option for # r, reword = use commit, but edit the commit message
我的解决方案是使用该r
选项# r, reword = use commit, but edit the commit message
So my commits list looked like this:
所以我的提交列表如下所示:
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
After saving, the interactive shell asked me for the rewording of the chosen commit.
保存后,交互式 shell 要求我重新编写所选提交。
After that, my commit log resulted in a single commit which resulted in a cleaner commit history.
在那之后,我的提交日志导致了一次提交,从而产生了更清晰的提交历史。
回答by DvixExtract
I had this problem and the reason why it happened in my case was that, you cannot squash older commits onto a new commit. Here is an example say you have 3 commits:
我遇到了这个问题,在我的情况下发生的原因是,您不能将旧提交压缩到新提交上。这是一个例子,说你有 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
Now if you say git rebase -i HEAD~3
and you do something like
现在如果你说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
This will result in the error:
这将导致错误:
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'.
错误:不能在没有先前提交的情况下“挤压”您可以使用“git rebase --edit-todo”修复此问题,然后运行“git rebase --continue”。或者您可以使用 'git rebase --abort' 中止变基。
Solution :
解决方案 :
When squashing commits, you should squash recent commits to old ones not vice versa thus in the example it will be something like this:
当压缩提交时,你应该压缩最近的提交给旧的提交,反之亦然,因此在示例中它将是这样的:
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
This will work fine, incase you want all your commit messages, I would suggest fixupinstead of squash.
这将正常工作,如果您想要所有提交消息,我建议fixup而不是squash。
回答by Kamafeather
Squash with the reverse logic. You'll be able to select the desired commit message at the later step.
用相反的逻辑挤压。您将能够在后面的步骤中选择所需的提交消息。
pick
the first commit that you don't wantthe commit-message for.squash
orfixup
the commit(s) that you want to merge, until the one that has the commit message that you actually wanted.
pick
您不想要提交消息的第一个提交。squash
或fixup
您想要合并的提交,直到具有您真正想要的提交消息的提交。
pick 56bcce7 Closes #2774
squash e43ceba Lint.py: Replace deprecated link
- confirm the change (
:x
) - delete the commit message(s) that you don't want and leave only the message from the commit that you desire(in this case:
Lint.py: Replace deprecated link
). - confirm the choice (
:x
)
- 确认更改 (
:x
) - 删除您不想要的提交消息,只留下您想要的提交消息(在这种情况下:)
Lint.py: Replace deprecated link
。 - 确认选择 (
:x
)
Hope it's clearer for somebody ?
希望对某人更清楚?
回答by ekyu88
It will be best to just say in the interactive editor containing the commits, git always squash from bottom to top and one should leave a "pick" entry at the top to receive the squashes from below.
最好只在包含提交的交互式编辑器中说,git 总是从下到上挤压,并且应该在顶部留下一个“选择”条目以从下面接收挤压。
回答by Tom Taylor
I just tried this approach.
我刚刚尝试了这种方法。
git log -n3
git log -n3
This would show the last 3 commits which would give me the idea of what is the latest commit and which one went previously. Now stated rebasing,
这将显示最后 3 次提交,这将使我了解什么是最新提交,以及之前提交过哪些。现在声明重新定位,
git rebase -i HEAD~3
git rebase -i HEAD~3
Choose the lastest commit on top of which we need to squash the other two. The commit id which is choosen as base commit would be like,
选择我们需要在其上压缩其他两个提交的最新提交。选择作为基本提交的提交 id 如下所示,
pick commit_id
选择 commit_id
For the other two commit ids, change them into,
对于另外两个提交 ID,将它们更改为,
squash commit_id
挤压 commit_id
or simply,
或者干脆,
s commit_id
s commit_id
回答by user8073722
I also have already meet with this problem just now,that's just careless.you can solve the problem like next:when you try to squash the first one(56bcce7) and pick the second one you should add "s" before the second line but not the first one. you can also reference the next web site:http://backlogtool.com/git-guide/en/stepup/stepup7_5.html
我刚才也遇到过这个问题,就是粗心大意。你可以这样解决问题:当你试图压扁第一个(56bcce7)并选择第二个时,你应该在第二行之前添加“s”但是不是第一个。你也可以参考下一个网站:http: //backlogtool.com/git-guide/en/stepup/stepup7_5.html