Blogs

Git Rebase: Keep your features' branches updated!

Introduction

Today's blog will discuss how you will keep your feature branch up-to-date with the main upstream branch.
Let's say you started working on a feature (a big one) and created a branch for it one month ago. Now, there are so many updates in the main upstream branch that you must include in your feature branch.
What is the easiest way to sync your feature branch with the upstream main branch?
The solution to this challenge is straightforward: run git rebase {branch-to-rebase-on}. This command effectively syncs your feature branch with the main upstream branch, ensuring you're always working with the latest updates.

Visualization

Let's visualize the rebase process. Suppose we have the following two branches, with the alphabets referring to each branch's commits. You created your feature branch when the main branch's head was at D.

A---B---C feature branch
/
D---E---F---G main branch

You can see now that the main branch's head is at the G commit. You need to change the HEAD of your feature branch to be on top of the G commit.

We run git rebase main, and the result will be:

A'---B'---C' feature branch
/
D---E---F---G main branch

And that's it; your feature branch now has the latest updates!

You might need to handle the merge conflicts if the main branch's updates conflict with your feature branch.
Make sure you do git pull origin main (or its equivalent) before running git rebase

Summary

Congratulations, you now know how to keep your branches up-to-date with your upstream!

References:

git-rebase