In my local GIT repository, I have the following commits:
A --> B --> C --> D
(Where D is the head)
I would like to combine/squash B and C into a single commit. How could I do that?
Thanks.
Use this command:
git rebase -i A ;# where A is A's commit hash
In the interactive editor, change the the lines, which should look something like this:
pick c7787af A
pick f9d9262 B
pick 6c363d0 C
pick 40f657d D
To this:
pick c7787af A
pick f9d9262 B
squash 6c363d0 C
pick 40f657d D
If you'd prefer to abandon C's commit message, but still squash the commit, use this:
pick c7787af A
pick f9d9262 B
fixup 6c363d0 C
pick 40f657d D
Never rebase a branch you've already shared with collaborators. The action changes SHA1 hashes, and results in non-fast-forward updates.
As Jason noted in comments, an alternative syntax is git rebase -i HEAD~4
. When you're rebasing recent history to squash commits (i.e. when it's easy to count parents back in time), this avoids the trouble of looking up A
's hash.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With