<aside> 💡 This guide will explain how to remove some commits from a Pull Request (or any branch for that matter) in order to slim the PR.

</aside>

When to squash a PR

Let's directly start with an example

As you can see, this PR has way too much commits for just a 6 line addition. It also has a more hidden problem: razorface has been added to the repository and then deleted, but since it is still in the history it will be pulled by the clients. While it is okay for just one file, it can be a real issue if someone commits their virtual environment, or something as bulky as that.

Since squashing a Pull Request is an exceptional operation, we don't allow squash merge, you will have to do it from the command line, let's jump right into it!

Squashing the Pull Request

To get started, you will have to check out the pull request branch on the remote. I personally recommend you to use the gh command line tool, but feel free to use what you prefer!

example-repository (main)> gh pr checkout 2
Switched to a new branch 'feat/001/add-duckies-to-readme'

Then, you should make sure that the python-discord master is up-to-date. Here, the python-discord remote is named upstream.

example-repository (feat/001/add-duckies-to-readme)> git fetch upstream

You are now all set! To get started, we will have to initiate an interactive rebase (thanks git!)

example-repository (feat/001/add-duckies-to-readme)> git rebase main -i

It will open your (favorite) editor with all the commits in the PR with the topmost commit being the first one.

pick 9a6229a add ducky_blue
pick 25e1533 add ducky_yellow
pick f06f890 add ducky_razorface
pick f6dfa69 add ducky_dave
pick a9e6aa6 add the duck collection header
pick 6f91b5f add dave to the duck collection
pick 5700b24 add razorface to the duck collection
pick e65ff1f add blue to the duck collection
pick 8dfb8ac add yellow to the duck collection
pick f20439f annihilate razorface

In order to squash all the commits in the PR, replace all the pick by squash, except for the topmost commit, like this:

pick   9a6229a add ducky_blue
squash 25e1533 add ducky_yellow
squash f06f890 add ducky_razorface
squash f6dfa69 add ducky_dave
squash a9e6aa6 add the duck collection header
squash 6f91b5f add dave to the duck collection
squash 5700b24 add razorface to the duck collection
squash e65ff1f add blue to the duck collection
squash 8dfb8ac add yellow to the duck collection
squash f20439f annihilate razorface

Switching the method to squash will ask Git to fuse this commit with the one above. You need to keep the first one as pick in order to have something to base the commit on, but do note that you can keep some pick in there to have a few more commits. Now, close your editor, and it should open again. (It may ask you for your PGP key password if you have one setup).

# This is a combination of 10 commits.
# This is the 1st commit message:

add ducky_blue

# This is the commit message #2:

add ducky_yellow

# This is the commit message #3:

add ducky_razorface
[Truncated...]

This will be the whole commit message for the new commit (every line starting by an hashtag will be ignored). I would recommend you to keep the individual commit name, but add a top message.