DEV Community

Ririio
Ririio

Posted on

Rebase is confusing

It was engraved into my mind the importance of doing a rebase. Having all commits compiled together into a singular one sounds amazing, and clean when sending out a pr.

Rebasing didn't sound bad, the steps were actually quite simple

  1. Push a commit everytime you completed a section of your overall task
  2. Once all is done, do a rebase interactive to main from your working branch and squash all of them together
  3. Checkout to your working branch and push it

The Problem

The problem occurs when someone reviews the code, and I wanted to pass my newly added code. The rebase interactive doesn't seem to contain anymore values. That's a problem, because if I'm going to be pushing my updated code, I will now have two commit as to one. It doesn't sound like that big of a deal, but if you have to push a code every time someone does a review, you'll end up with several to up to a dozen commits on your pull request like this pr did.

How to solve the issue

The steps I told at the start was not all wrong, but if you noticed one thing, I actually never do a pull upstream. What do you think would happen if I pass a code that is behind the main branch? I would have conflicts. If I simply keep on doing what I did with rebase, I will consistently have conflicts. Now you're asking, what are the actual steps onto doing a proper rebase? the same steps apply

  1. Push a commit every time you completed a section of your overall task
  2. Once all is done, do a rebase interactive to main from your working branch and squash all of them together git rebase main -i

Now you add extra steps...

  1. Do a checkout to the main branch git checkout main
  2. Update your main branch to its latest instance through git pull upstream main
  3. Checkout back to your working branch

Now instead of pushing it, you do another rebase but there's no need for interactive, because you only have a single commit

  1. Remember to do a force push, because you are trying to alter a previous branch history git push -f origin main

Always make sure that before you do anything, that your main branch is always clean. If you are not sure simply do a git checkout -B main upstream/main, this should reset your main to the latest commit from the original repo.

Remember that upstream is only for fork repo, you should be using origin from your own

Top comments (0)