[英]git rebase cannot 'squash' without a previous commit
I used to use git rebase -i
all the time, but suddenly it stopped working. 我曾经一直使用git rebase -i
,但是突然它停止了工作。
git rebase
does not work in any project I have, which is weird and I suspect it's something wrong with my git configuration(?) git rebase
在我拥有的任何项目中均不起作用,这很奇怪,我怀疑git配置有问题(?)
I've createad a test
repository to show what I'm doing. 我已经创建了一个test
库来显示我在做什么。
In the test repository I have 2 commits 在测试库中,我有2次提交
$ git log $ git日志
commit 8fb921a9a481ef1040ee670af7894bff6055a5b4 (HEAD -> master, origin/master)
Author: Bu Kinoshita
Date: Fri May 25 11:26:05 2018 -0300
test 2
commit 00ffa75caccf0118b9e89bc2bb70c4a7417b223a
Author: Bu Kinoshita
Date: Fri May 25 11:25:39 2018 -0300
test
And then git rebased
with commit hash from the first commit 然后git rebased
从第一次提交git rebased
用提交哈希值重新建立基础
git rebase -i 00ffa75caccf0118b9e89bc2bb70c4a7417b223a git rebase -i 00ffa75caccf0118b9e89bc2bb70c4a7417b223a
pick 8fb921a test 2
# Rebase 00ffa75..8fb921a onto 00ffa75 (1 command)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
Change the first line to squash 8fb921a test 2
and save the file. 将第一行更改为squash 8fb921a test 2
并保存文件。
The error shows up 错误出现
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'.
In the test repository I have 2 commits 在测试库中,我有2次提交
To squash the second commit into the first one using git rebase
, you must rebase both commits. 要使用git rebase
将第二个提交压缩为第一个提交,您必须将两个提交重新设置为基准。
Since there are only the two, git rebase -i
alone won't work. 由于只有这两种,所以单独使用git rebase -i
行不通的。 Using git rebase -i --root
will. 使用git rebase -i --root
将。
However, since there are only the two commits, and the commit you want to squash is the current commit, you can do this without using git rebase
at all: 然而,由于只有两个提交,并希望壁球犯是当前提交,你可以做到这一点,而无需使用git rebase
在所有:
git reset --soft HEAD~1
git commit --amend
The drawback here is that --amend
will only present the first commit's message for editing; 这里的缺点是--amend
仅会显示第一次提交的消息以供编辑; the second commit's message will be gone at this point. 此时第二次提交的消息将消失。 Note that --amend
doesn't actually change the existing commit; 注意,-- --amend
实际上并不会改变现有的提交; instead, it makes a new commit whose parents are the current commit's parents. 相反,它将做出一个新提交,其父作为当前提交的父。 In this case, the current commit is the root commit, so this uses the root commit's lack-of-parents to make a new root commit. 在这种情况下,当前的提交是根提交,因此它使用了根提交的父母不存在来进行新的根提交。
your just squashing in the wrong direction. 你只是朝错误的方向挤压。 squash upwards instead of downwards ..so if you had: 向上挤压而不是向下挤压..所以,如果您有:
Pick ####
Pick ###
Pick ###
then squash into one commit this way: 然后以这种方式压入一个提交:
Pick ###
Squash ###
Squash ###
this way there is a commit before . 这种方式之前有一个提交。 you can use the reword instead of pick if you need to rename the commit. 如果您需要重命名提交,则可以使用reword而不是pick。 but a screen will come after you save were you can edit the commit message that the PR review will see 但是保存后会出现一个屏幕,您可以编辑PR审阅会看到的提交消息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.