-
Notifications
You must be signed in to change notification settings - Fork 0
How to squash multiple commits
how to squash commits during the PR processes (from chatgpt): Combining multiple commits into one is known as "squashing" in Git terminology. Here's how you can squash multiple commits into one for a cleaner PR:
Interactive Rebase: Use the git rebase command in interactive mode (-i). Start the rebase from a commit before the ones you want to squash. git rebase -i HEADn Replace n with the number of commits you want to go back. For instance, if you want to squash the last 5 commits, you would use HEAD5.
Squashing in the Editor:
After running the command above, an editor (often Vim or Nano, depending on your configuration) will open, displaying a list of commits:
pick a1b2c3d First commit pick e4f5g6h Second commit pick h7i8j9k Third commit ... To squash commits, replace the word pick with the word squash or just s next to the commits you want to squash into the previous commit. If you want to squash "Second commit" and "Third commit" into "First commit", it would look like this:
pick a1b2c3d First commit squash e4f5g6h Second commit squash h7i8j9k Third commit ... Save and close the editor.
Combining Commit Messages:
The editor will reopen, allowing you to combine the commit messages. Edit the commit message to be clear and descriptive, then save and close the editor again.
Pushing to GitHub:
If you've already pushed your branch to GitHub, you'll need to force push the branch after squashing: ` git push origin YOUR_BRANCH_NAME --force-with-lease
` Warning: Force pushing can overwrite changes on the remote that you don't have locally. Using --force-with-lease is safer than --force as it ensures you don't overwrite someone else's work by accident. Always be cautious when force pushing.
Review the PR:
Go back to your PR on GitHub and ensure everything looks as expected. The multiple commits should now be combined into the single squashed commit.
Remember, squashing is a form of history rewriting. Only squash commits on branches/features that haven't been merged to main shared branches (like master or main). Otherwise, it could lead to confusion and conflicts for others collaborating on the same branch.