简体   繁体   中英

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 does not work in any project I have, which is weird and I suspect it's something wrong with my git configuration(?)

I've createad a test repository to show what I'm doing.

In the test repository I have 2 commits

$ git log

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 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.

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

To squash the second commit into the first one using git rebase , you must rebase both commits.

Since there are only the two, git rebase -i alone won't work. Using git rebase -i --root will.

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 reset --soft HEAD~1
git commit --amend

The drawback here is that --amend will only present the first commit's message for editing; the second commit's message will be gone at this point. Note that --amend doesn't actually change the existing commit; 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. but a screen will come after you save were you can edit the commit message that the PR review will see

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM