Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ntung/635d3baf897baefb1dd124d01028697e to your computer and use it in GitHub Desktop.
Save ntung/635d3baf897baefb1dd124d01028697e to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment