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

Dawny33 picture Dawny33 · Sep 20, 2016 · Viewed 84.8k times · Source

I have the following in the to-do text of 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:

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

Answer

Leon picture Leon · Sep 20, 2016

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

  • git rebase -i HEAD~3 (if you want to squash 56bcce7 into 684f917)
  • If you mean to combine 56bcce7 with e43ceba, and e43ceba doesn't depend on 56bcce7, then simply reorder them:

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

    UPDATE: Gus's answer below 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 of 56bcce7 and e43ceba.