Git Merge and Rebase: Everything You Need To Know

Git rebase solves the same problem as git merge. Both git merge and rebase are used to integrate changes from one branch into another branch – they just do it in different ways; and it’s important to know the difference.

Hey there, fellow coders! Today, we’re diving into a topic that’s caused many a headache in the world of version control: git merge and rebase. Now, I know what you’re thinking – “Ugh, not another git tutorial!” But stick with me here, because understanding the difference between these two can seriously level up your collaboration game.

I remember when I first started using Git, I’d break out in a cold sweat every time I had to integrate changes from different branches. It felt like I was performing surgery on my codebase! But over time, I’ve come to appreciate the nuances of merge and rebase, and I’m excited to share what I’ve learned with you. So grab your favorite caffeinated beverage, and let’s unravel this git mystery together!

Imagine you have a project and you’re working on it with a few friends. You all make changes to the project and save your changes in something called a “branch.” A branch is like a separate copy of the project that you can work on without affecting the main project. When you’re done making changes to your branch, you can ask Git to “merge” your changes back into the main project. Git will take all of the changes you made in your branch and add them to the main project, making it easy for everyone to see and use your changes.

Rebasing is a way to merge changes from one branch into another, but it’s a little different from the regular merge process. When you rebase, Git will take all of the changes you made in your branch and apply them one by one to the main project, as if you had made the changes directly to the main project. This can make it easier to see what changes you made and how they fit into the main project.

Let’s think of a scenario.

Our master branch (m) have commit: 1 -> 2 -> 3. Now we need to develop a feature. So we created our own feature branch (f) from the master branch with the latest commit no: 3. We are working on our feature and pushed 3 commits no: 5, 7 and 9. In the meantime; master branch remains busy and commit 4, 6 and 8 was pushed by our amazing team members.

Now at some point; we might need to do 2 things:

  1. Your feature development is finished and you need to push your work (commit 5,7 & 9) of feature branch (f) to master branch (f);
  2. Bring the things that has been pushed to master branch (m) (commit 4,6 & 8) into your own branch (f), and then continue to work on the branch (f).


Consider the first scenario. And let’s consider ‘merge’ route. That is we go to master branch and merge the feature branch with the master with the commands –

git checkout master
git merge feature

By merging feature into master, master obtains a new commit — a ‘merge commit’. This commit contains everything from our commit – (5+7+9) – into a combined sort of commit (10*) and it appends itself after commit 8 on master.


Now let’s consider the second scenario. And this time we will go with ‘rebase’ route. We will bring all the magical commits pushed into master (m) into our branch (f). Keep in mind; the current commit history for our branch is: 1-> 2-> 3-> 5-> 7-> 9. Now we rebase with master with the following command –

git checkout feature
git rebase master

Now this part is interesting. Rebase moves the base of our branch. The process is this –

  1. First it brings all the commits from master (1-> 2-> 3-> 4-> 6-> 8)
  2. And applies our commits (5-> 7-> 9); after this base commit 8; one by one to create (5′ -> 7′ -> 9′)

So the new history for our branch (f) becomes – 1-> 2-> 3-> 4-> 6-> 8 -> 5′-> 7′-> 9′. It does not create a new all-in-one sorts of commit; rather it rewrites the history of the branch. Note that; commit 5,7,9 is same as 5′,7’9′ to us but git treats them as new commits (with new SHA’s).

Now here comes the tricky part.
Say our good friend Rafi is working on the same feature. He checked out the feature branch after we pushed the commit 5; and he has committed 10 & 11. So; his commit history looks like this –

1-> 2-> 3-> 5-> 10 -> 11.

In the meantime; we changed the history of feature branch to –

1-> 2-> 3-> 4-> 6-> 8 -> 5′-> 7′-> 9′.

Now whether Rafi wants to bring back master branch commit into his own copy of feature branch; update his copy of feature branch with our feature branch or push his commit into the master branch; there is potential for some conflict and issues; and all because git rebase rewrote the history of the feature branch unlike merge.

git merge and rebase
So; golden rule of thumb of using rebase is

don’t rebase a branch unless you are the only one who are using it.

If we want to control and alter commits as they are moved to the new branch while rebasing; we can use Interactive Rebasing. The command for this is –

git checkout feature git rebase -i master

This will open a text editor listing all of the commits that are about to be moved. Here the listing defines what the branch will look like after the rebase is performed. By changing the pick command and/or re-ordering the entries, we can make the branch’s history look like whatever we want.

For example, if the 2nd commit fixes a small problem in the 1st commit, we can condense them into a single commit with the fixup command. Eliminating insignificant commits like this makes feature’s history much easier to understand. This is a benefit of rebase that git merge simply cannot do.

One advantage of using git merge is that it’s easy to understand and use. It’s a simple way to combine changes from multiple branches, and it’s especially useful when you want to bring all of the changes from one branch into another without any special treatment.

However, git merge can sometimes create “merge conflicts,” which occur when the same lines of code have been changed in both branches. In this case, Git will ask you to resolve the conflict manually by deciding which changes to keep and which to discard. This can be time-consuming and may require some technical knowledge to resolve.

When you use git rebase, Git will apply the changes from the branch you want to merge one by one, as if you had made them directly to the branch you’re currently on. This can make it easier to see how the changes fit into the project, and it can also help you avoid merge conflicts.

One advantage of using git rebase is that it can help you avoid merge conflicts, since the changes are applied one by one. This can make it easier to see how the changes fit into the project and resolve any conflicts that may arise.

However, git rebase can be more complex to use than git merge, and it’s generally not recommended for beginners. It can also be more difficult to understand the history of your project when using git rebase, since the commits are reordered and combined into new ones.

Now, let’s take a moment to chat about when you might choose one method over the other. It’s not just about personal preference – there are real-world scenarios where merge or rebase might be the better option.

Imagine you’re working on a long-running feature branch. You’ve been coding away for weeks, and suddenly you realize you need to pull in some changes from the main branch. This is where rebase shines. By rebasing your feature branch onto the latest main, you’re essentially saying, “Hey, pretend I started my work from this point instead.” It keeps your feature branch nice and tidy, without those pesky merge commits cluttering things up.

On the flip side, let’s say you’re wrapping up a feature and getting ready to merge it back into the main branch. This is where a good old-fashioned merge comes in handy. It preserves the entire history of your feature development, merge commits and all. This can be super helpful if you ever need to track down when and why certain changes were made.

The key is to be flexible and use the right tool for the job. And remember, whichever method you choose, communication with your team is crucial. Nothing ruins a developer’s day quite like a surprise force push or a massive merge conflict!

Pros of merge –
  • Simple and familiar
  • Preserves complete history and chronological order
  • Maintains the context of the branch
  • Makes it easy to bring changes from one branch into another
  • Maintains a clear history of your project, with separate commits for each change
Cons of merge –
  • Commit history can become polluted by lots of merge commits
  • Debugging using git bisect can become harder
  • Can create merge conflicts, which can be time-consuming and difficult to resolve
  • May result in a cluttered commit history if you merge branches frequently
Pros of Rebase –
  • Streamlines a potentially complex history
  • Manipulating a single commit is easy
  • Avoids merge commit “noise” in busy repository with busy branches
  • Cleans intermediate commits by condensing them into a single commit
  • Can help you avoid merge conflicts by applying changes one by one
  • Makes it easier to see how changes fit into the project
Cons of rebase –
  • Squashing the feature down to a handful of commits can hide many important details.
  • Rebasing public repositories can be dangerous when working as a team.
  • It’s more work using rebase to keep our feature branch updated always.
  • Rebasing with remote branches sometimes requires force push which may cause problems if not done properly.
  • More complex to use than git merge
  • Can be confusing for beginners
  • Can make it harder to understand the history of your project, since commits are reordered and combined into new ones
  • If you rebase a branch that has already been pushed to a remote repository, it can cause problems for other collaborators who have based their work on the original commits.

Git Merge and Rebase: Summary

Create a feature branch and then –

  • ALWAYS rebase from master to the branch. (merge is fine too)
  • And merge from the branch back to master. (avoid rebasing).

Whew! We’ve covered a lot of ground today, haven’t we? From the basics of merge and rebase to the nitty-gritty details of when to use each one. But before we wrap up, I want to share a personal anecdote that really drove home the importance of understanding these concepts.

A few years back, I was working on a project with a distributed team. We were all merrily coding away, merging and rebasing with abandon, when disaster struck. One of our devs had rebased a shared branch and force pushed it to the remote repo. Chaos ensued. Half the team couldn’t push their changes, merge conflicts popped up left and right, and we spent the better part of a day untangling the mess.

That experience taught me two valuable lessons:

  1. Always, always communicate with your team about your Git workflow.
  2. Don’t rebase shared branches unless you really know what you’re doing (and even then, think twice!).

Remember, Git is a powerful tool, but with great power comes great responsibility. Whether you’re Team Merge, Team Rebase, or somewhere in between, the most important thing is to use these tools thoughtfully and consistently.

So, the next time you’re faced with the age-old question of “to merge or to rebase?”, take a deep breath, think about your specific situation, and choose wisely. Your future self (and your teammates) will thank you!

Happy coding, and may your repositories always be conflict-free!

Happy coding!

Leave a Comment