If you are opening pull requests for other projects it might happen that you are asked to reduce the amount of commits of your pull request in order to refine the history of your contribution. That usually means that you squash together existing commits and you might even change some commit messages to be more meaningful or precise. That kind of workflow is supported by git rebase.

git rebase - Rewriting the commit history

There are a lot of great tutorials talking about the power of git rebase and I’d recommend to check these out if you are unfamiliar with that command. To rewrite our history we are going to use the interactive rebasing: git rebase -i.

  1. The first we are doing is switching to our feature branch we created for the pull request:

     git checkout feature-branch-x
  2. No we start the rebasing on top of the branch we come from. This could be master or maybe dev. With on top of, I mean the branch/commit the feature branch is based on.

     git rebase -i master
  3. Your favourite editor should open and you can define the rebase strategy.

  4. After this step is done you have to update the commit history of your branch by pushing.

     git push --force

    It is important to use the --force option to overwrite the existing history!

  5. Now you only have to inform the contributor about your rebasing, because Github does not support notifications for rebased pull requests.

Congratulations, you just took another step on the way to mastering git. :)