Understanding Git Merge and Git Rebase

Understanding Git Merge and Git Rebase

As a developer, many of us have to choose between git merge and git rebase. In this blog, I will make you clear everything about both the commands, and then you will be able to decide for yourself when to use and which command to use.

The first thing to understand is that both the commands git rebase and git merge serves the same purpose. Both of these commands are designed to integrate changes from one branch into another branch — they just do it in very different ways.

Supposed you are working on a feature on a dedicated branch. Meanwhile, someone committed some changes on the main branch which results in a forked history, which should be familiar to anyone who has used Git as a collaboration tool.

1__L9yu9ihA0XExvZoJPKHXw.png

Now let’s say that the new commit is somehow relevant to the feature you are working on. You have two options to incorporate the new commits into your feature branch: merging or rebasing.

Git Merge

1_rIf5sdB4N40q7CggBsj2lA.jpeg

Merging is a common practice for developers using version control systems. Whether branches are created for testing, bug fixes, or other reasons, merging commits changes to another location. To be more specific, merging takes the contents of a source branch and integrates them with a target branch. In this process, only the target branch is changed. The source branch history remains the same.

How to do it?

  $ git checkout feature
  $ git merge master

or in one line you can do

$ git merge master feature

Pros and Cons of using git merge?

Pros:

  • Merging is nice because it’s a non-destructive operation.
  • The existing branches are not changed in any way. This avoids all of the potential pitfalls of rebasing (discussed below).
  • Preserves complete history and chronological order.

Cons:

  • If the main branch is very active, this can pollute your feature branch’s history quite a bit. While it’s possible to mitigate this issue with advanced git log options, it can make it hard for other developers to understand the project’s history.

Git Rebase

1_NpEOBwe5k7wUuVQZa4hLuw.jpeg

Rebase is another way to integrate changes from one branch to another. Rebase compresses all the changes into a single “patch.” Then it integrates the patch onto the target branch. Unlike merging, rebasing flattens the history because it transfers the completed work from one branch to another. In the process, unwanted history is eliminated.

How to do it?

$ git checkout feature
$ git rebase master

Pros and Cons of using git rebase?

Pros:

  • The primary benefit of rebasing is that you get a much cleaner project history.

  • Avoids merge commit “noise” in busy repositories with busy branches.

  • Manipulating a single commit is easy (e.g. reverting them).

  • Rebasing also results in a perfectly linear project history — you can follow the tip of the feature all the way to the beginning of the project without any forks.

Cons:

  • Squashing the feature down to a handful of commits can hide the context.

  • Rebasing public repositories can be dangerous when working as a team

  • It’s more work: Using rebase to keep your feature branch updated always.

-You rebase incorrectly and unintentionally rewrite the history, it can lead to severe issues, so make sure you know what you are doing!

Which one to use?

One needs to consider several questions when setting their Git to rebase vs. merge policies. Because as it turns out, one workflow strategy is not better than the other. It is dependent on you and your team.

What would I recommend from my personal experience is to avoid using git rebase when you are working in a team.

Conclusion

I hope this blog has given some insights on git merge and git rebase. To know more about git commands check out this article.

If you have any questions or feedback let me know in the comment section down below. And we connect on Twitter and LinkedIn for more updates on my journey in tech.