CodeNewbie Community 🌱

Thais Cristine
Thais Cristine

Posted on • Updated on

Rebase? Force push?!

You being a newbie like me, might be concerned about rebasing or doing a force push even when instructed to do so on the project you are working on. Well, you might face it at some point, and let's make sure you understand what is going on and do it safely without double thoughts.
Alt Text

Think that after rebasing, you have a copy of the project commits locally, so when you try to push it, the server will reject because they basically don't get along. That's why we need to force push it. When you rebase and force push, you are overwriting the history. Be sure no one else is working on the same branch.

Okay, if you are now pondering what the heck all the above meant - please read further!

First things first: when do I need to rebase?

Rebasing happens mostly like when you started a new branch out of the master, put some work on it, and needed to have a pause on this feature for a while (critical problem? Houston, we have a problem?). Things come down, and you came back to work on your lovely beach and realized that the master branch from where you checkout out your branch initially has moved forward, and it has new content on it. You don't want to lose your work, but you need to be updated within the master branch to merge your content. Time to rebase!

git checkout your-working-branch
git rebase master

In a beautiful perfect world, no conflicts came out of it, and you are good to go. Most probably, you got some conflicts but still no need to freak out. I got you.

The warning is letting you know what file has a conflict. Open it to check. I use VisualCode for this checkup because it has a self-explanatory interface presenting the incoming and current code. Edit the file as you please, giving preference for the received code or your own changes and save it. Now we need to add and commit it and keep going with the rebase.

git add path/to/your/file.ts
git rebase --continue

Having more conflicts to solve, you should do the same steps above until finishing your rebase. It happens normally many times because the rebase goes through each commit done.
If at any moment you are unsure about what you are doing and would like to stop, you can abort your rebasing

git rebase --abort

Rebase finished, new changes made, and committed; now it's time to push. Why doesn't the server allow me to push?

As said in TL;DR, you have a copy of the same commits locally, and it's not interesting for the server to duplicate it. That's why we need to use the flag -f to force push the local work. You probably want to use even --force-with-lease instead of just -f for less destructive cousin :)

git push origin your-working-branch --force-with-lease

There you go! The commits should by now be rewritten within the rebase commits included. Hopefully, I could help you somehow!

** During Rebase **
current change = current content of the master branch from where you are rebasing. You don't have it yet, do you want it?

incoming change = that's the code you have locally, the gold work you have already done and wouldn't like to lose during rebasing. * Are you sure you prefer the current change other than me? *

Top comments (0)