Here's a quick guide to squash commits from HEAD~2
to HEAD~4
(including these commits) into a single commit:
-
π Start Interactive Rebase from
HEAD~5
:git rebase -i HEAD~5
This command starts an interactive rebase from the 5th commit back from
HEAD
, allowing you to edit commits fromHEAD~2
toHEAD~4
. -
βοΈ Edit the Commit List:
- Git will open a text editor showing a list of commits.
- Change
pick
tosquash
(ors
) for each commit you want to merge into the first one. - Keep
pick
on the first commit in the series, then mark subsequent commits withsquash
.
Example:
pick <commit_id_1> Initial Commit message squash <commit_id_2> Update tests squash <commit_id_3> Adjust styles pick <commit_id_4> Refactor code pick <commit_id_5> Fix small bug
-
πΎ Save and Close:
- Save and close the editor to start the rebase process.
-
π Edit the Squashed Commit Message (optional):
- Git will open the editor again to let you edit the final message.
- Combine messages or create a new one for the squashed commit, with emojis if desired!
-
π Push Changes with
--force
:git push origin main --force
- Use
--force
because you've rewritten the history.
- Use
And that's it! π Youβve successfully squashed commits from HEAD~2
to HEAD~4
into one.