简体   繁体   English

如果没有先前的提交,git rebase不能“压扁”

[英]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.

相关问题 Git:变基时“无法在没有先前提交的情况下‘挤压’”错误 - Git: "Cannot 'squash' without a previous commit" error while rebase 错误:如果没有先前的提交就无法“挤压”您可以使用“git rebase --edit-todo”修复此问题,然后运行“git rebase --continue” - Error: cannot 'squash' without a previous commit You can fix this with 'git rebase --edit-todo' and then run 'git rebase --continue' git rebase并用一条消息压缩提交 - git rebase and squash the commit with a message 将更改添加到git上的先前提交而无需重新设置 - Add changes to previous commit on git without a rebase 为什么'Git rebase'压缩提交到先前的提交,而不是以下提交? - Why does 'Git rebase' squash commits into a previous commit, instead of the following commit? 使用“git merge --squash”样式提交消息在 git rebase 中挤压? - Squash in git rebase with "git merge --squash" style commit message? Git:在同一操作中添加,提交和变基/压缩 - Git: add, commit and rebase/squash in same operation Git Interactive变基:将文件压入更改的提交中 - Git Interactive rebase: Squash files to the commit they changed git commit / rebase / squash的单个命令 - Single command for git commit/rebase/squash git 交互式变基压缩到下一次提交 - git interactive rebase squash into next commit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM