Blogs


Should you use git merge or git rebase?

 January 29, 2021, 06:44 PM

 5 min read

git
github

Well.. what is git?
Git is a version control software which developers can use to keep track of a project or some code. It's best use case is with moderately simple sized projects to large and complex projects, but I would advise beginners to learn git early on and to start implementing it on their little projects as it will help to build a solid foundation about how git works. I will admit, I was very late to the git scene.... and I regret it!

Ok so why use it?
Well, picture this - You are working on a relatively simple project. You have a foundation for that project. One day, while having lunch, you came up with a brilliant idea to add an all new state of the art feature but you aren't exactly sure how you will achieve it. This is where git comes in handy. Using git, you can create a new feature branch, where you will design, create and test this feature but you wont push the feature to your stable main (or master as called in the world of git) branch until you are ready to release it.

THE FEATURE IS FINALLY READY.
You merge your feature branch with your master (or main) branch and push it to production. Everything is going great. Life is good.

THE FEATURE IS FILLED WITH BUGS AND HACKERS ARE EXPLOITING IT.
All hell break loose. You forgot to test it thoroughly (rookie mistake), and there appears to be a massive bug and hackers are exploiting it. But you aren't even exactly sure what the bug is. Well, whatever it is, first thing you need to do is roll-back the production version. But that will be a huge pain, wouldn't it? After all, this WAS a state of the art feature so it probably involves many lines of code. Well, fear not warrior for git is here to save you. With git, you can roll back this feature commit with ease so you can start from before the merger of the two branches and work on the feature branch all over again.


Ok so lets get to the topic of this blog - Merge or rebase. Lets try to picture the situation described above in a diagram.

Merge
Merging is when the deviant branch, or the feature branch in this case, is merged with the main branch. I.e, the main branch is updated with the feature.

When to merge?
Ideally, you would want to merge once you are finished working on a certain branch or feature. This type of feature can scale from large project breaking features to small utility convenience features.
How to merge?

git checkout main  
git merge feature

And you are done! It's that simple. Note that in this example, "main" branch is the base master project and the "feature" branch is the feature branch to be implemented and merged with the rest of the project.

Rebase
Rebase is when you want to essentially rewrite the history of one branch with respect to another branch. For example, from the previous diagram, we can see that after the feature branch was created, there was another new commit in the main branch which is not present in the feature branch. With rebase, you can "re-write" or rebase the feature branch to include the new commit in the main branch.
If you were to rebase the feature branch with respect to the main branch, it would look some like this-

How to rebase

git checkout feature  
git rebase main

And you are done! Note that ideally, you would NOT want to rebase the main/public branch as re-writing it's history can have other negative effects. Even though reading the history will be easier as it will be a linear flow, it can potentially lose context of some commits that merge would normally provide and make it harder for newer developers to understand the project history.


Arafat

Mohammad Arafat Zaman

"Technophile"


Go back

Mohammad Arafat Zaman © 2024


All rights reserved