2

I have two commits locally:

commit adae40c5e2b69a41447b08cc3dcb77003611fbbe
Author: Me
Date:   Thu Mar 21 14:17:35 2019 +0000

    1.0.0

commit ceaa65ea06f48dc24554a6f798aae2d668f3a43d
Author: Me
Date:   Fri Feb 1 10:04:36 2019 +0000

    first commit

How do I squash these so there is only one commit with the message 1.0.0?

I tried the following but neither has worked for me:

git rebase -i HEAD~2
git rebase -i master
3
  • Did you change the pick to squash for the second commit in the opening editor window?
    – kowsky
    Commented Mar 21, 2019 at 14:29
  • stackoverflow.com/search?q=%5Bgit-rebase%5D+first+commit
    – phd
    Commented Mar 21, 2019 at 14:51
  • 2
    In your specific situation, RomainValeri's answer is probably the simplest solution. However, it may be useful to understand why interactive rebasing isn't working for you, because rebase is a much more powerful / more general tool and will work in many cases where simply reset + commit will not Commented Mar 21, 2019 at 15:18

1 Answer 1

8

For those not fond of interactive rebasing, in a situation like this it's also quite easy to just rewind and recommit :

git reset --soft HEAD~2
git commit -m "1.0.0"
3
  • 2
    Rebasing is an especially helpful feature in Git but it will not squash commits, it will do the opposite and preserve each commit. The reset then commit process you laid out is perfect for squashing the commit history.
    – benhorgen
    Commented Mar 21, 2019 at 14:38
  • 1
    @benhorgen You're right. It's unclear from my answer but I did refer to interactive rebasing (then choosing squash where it should) rather than "normal" rebasing. Commented Mar 21, 2019 at 14:43
  • @benhorgen - I'm not sure what you mean. A rebase can indeed squash commits. It is true that the original commits are preserved (at least temporarily) after any rebase, but the same is true of the rebase-and-recommit method. Commented Mar 21, 2019 at 15:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.