Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Squash the first two commits in a git repository's history

The scenario

Your repository has two commits:

$ git log --oneline
957fbfb No, I am your father.
9bb71ff A long time ago in a galaxy far, far away....

Use the interactive rebase tool to squash the two commits:

$ git rebase -i 9bb71ff

When your editor opens, only a single commit is listed:

pick 957fbfb No, I am your father.

You change pick to squash, save & close your editor.

The problem

Git complains...

Cannot 'squash' without a previous commit

The fix

$ git rebase -i 9bb71ff

This time, when your editor opens, change pick to edit instead of squash, save & close your editor.

$ git reset --soft HEAD^
$ git commit --amend

Your editor again so that you can modify the commit message of the soon-to-be squashed commit; make your changes, save & close the editor.

$ git rebase --continue
@cpicanco

This comment has been minimized.

Copy link

@cpicanco cpicanco commented Oct 4, 2016

Excelent, thank you for this!

@georgegillams

This comment has been minimized.

Copy link

@georgegillams georgegillams commented Mar 6, 2018

🙇‍♂️

@rnmulchandani

This comment has been minimized.

Copy link

@rnmulchandani rnmulchandani commented Oct 23, 2018

Superb 👍

@p-m-m-c

This comment has been minimized.

Copy link

@p-m-m-c p-m-m-c commented Jan 15, 2019

Thanks! Short explanation on the internals of this operation? 😁

@nineSean

This comment has been minimized.

Copy link

@nineSean nineSean commented Jan 23, 2019

the simplest way is git reset --soft head^ then git commit --amend without using git rebase

@m1koop

This comment has been minimized.

Copy link

@m1koop m1koop commented Jun 13, 2019

Thanks very much for this... will bookmark this link for sure.

@akhan3

This comment has been minimized.

Copy link

@akhan3 akhan3 commented Jun 26, 2020

👍

@a37h

This comment has been minimized.

Copy link

@a37h a37h commented Nov 4, 2020

@nineSean thanks

@digitaljhelms

This comment has been minimized.

Copy link
Owner Author

@digitaljhelms digitaljhelms commented Nov 4, 2020

@nineSean 👍 💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.