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

前端 未结 7 601
逝去的感伤
逝去的感伤 2021-01-29 21:06

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 68         


        
7条回答
  •  庸人自扰
    2021-01-29 21:54

    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:

    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

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

    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 fixup instead of squash.

提交回复
热议问题