Skip to content

Instantly share code, notes, and snippets.

@digitaljhelms
Created July 12, 2012 15:58
Show Gist options
  • Select an option

  • Save digitaljhelms/3099010 to your computer and use it in GitHub Desktop.

Select an option

Save digitaljhelms/3099010 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
@cpicanco

cpicanco commented Oct 4, 2016

Copy link
Copy Markdown

Excelent, thank you for this!

@georgegillams

Copy link
Copy Markdown

πŸ™‡β€β™‚οΈ

@rnmulchandani

Copy link
Copy Markdown

Superb πŸ‘

@p-m-m-c

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

Copy link
Copy Markdown

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

@nineSean

Copy link
Copy Markdown

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

@m1koop

m1koop commented Jun 13, 2019

Copy link
Copy Markdown

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

@akhan3

akhan3 commented Jun 26, 2020

Copy link
Copy Markdown

πŸ‘

@quasa0

quasa0 commented Nov 4, 2020

Copy link
Copy Markdown

@nineSean thanks

@digitaljhelms

Copy link
Copy Markdown
Author

@nineSean πŸ‘ πŸ’―

@Amoong

Amoong commented Feb 25, 2021

Copy link
Copy Markdown

😍

@ethfun

ethfun commented Jul 15, 2021

Copy link
Copy Markdown

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

πŸ‘

for anyone just want to modify commit Author, then you can do
git reset --soft head^
git commit --amend --author="Author Name <email@address.com>" --no-edit

@seandenigris

Copy link
Copy Markdown

Awesome! Thanks :)

@johnrichardrinehart

Copy link
Copy Markdown

Thanks!

@a-andreyev

Copy link
Copy Markdown

Thanks! In case of an issue with zsh: zsh: no matches found: HEAD^, don't forget to escape the ^ symbol with \:

git reset --soft HEAD\^`

@dax-er

dax-er commented Feb 15, 2024

Copy link
Copy Markdown

You can also use HEAD~1 if HEAD^ doesnt work

@frenzymind

Copy link
Copy Markdown

I just do it simpler:

git rebase -i HEAD~N , where N - commits count in your branch (including first one)

and then do any operations you usually do in interactive mode

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment