How to combine the last two commits into a single one in Git
If you want to squash the last two commits into one without changing the commit message, you can follow these steps:
Steps to Squash the Last Two Commits Without Changing the Commit Message
- Start an Interactive Rebase: Use the git rebase -i command to start an interactive rebase for the last two commits:
git rebase -i HEAD~2 - Edit the Rebase File: In the text editor that opens, you’ll see something like this:
pick a1b2c3 First commit message
pick d4e5f6 Second commit message
Change the second pick to squash (or s) to indicate you want to squash it into the first commit:
pick a1b2c3 First commit message
squash d4e5f6 Second commit message - Save and Close the Editor: After making the change, save and close the editor.
- Keep the Commit Message: When Git opens another editor for the commit message, it will show you both commit messages. You can simply delete the second commit message and leave the first one as is, or you can keep both messages without any modifications. If you want to keep the first commit message exactly as is, just make sure it appears correctly.
For example, it might look like this:
First commit message - Save and Close the Editor Again: Save and close the editor again to complete the rebase.
- Force Push (if applicable): If you had previously pushed the original commits to the remote repository, you will need to force push the changes:
git push origin HEAD --force
Comments
Post a Comment