When you are using git, most probably you may used git merge or git rebase to combine two branches. Pretty much both of these commands do the same thing, but in a different way. This post is to compare the difference between git merge vs rebase and when to use.


Think about what happens if you’re working on a new feature in a dedicated branch and another team member updates the master branch with new commits. This leads to a split-off history.

A forked commit history


Let’s say that the new commits in master are relevant to the feature you’re working on. To integrate the new commits into your feature branch, you have two options: merging or rebasing.

the second variante also, Once you completed the feature implementation on feature branch, you need to combine it with the latest master branch. Either you can merge or rebase feature branch with the master.

Die Merging-Option

Let´´ s say we are merging branche “master” into the feature branch , This creates a new “merge commit” in the feature branch that ties together the histories of both branches, giving you a branch structure that looks like this:

Merging master into the feature branch

Merging is nice because it’s a non-destructive operation. The existing branches are not changed in any way. 

After a merge, we have a single new commit on the branch we merge into. This commit contains all the changes from the source branch.

You can merge master branch into the feature branch by running following command:

#Merge aus Master nach Feature

git checkout feature
git merge master

or with single line

git merge feature master

#Merge aus Master nach Feature, bei dem im Konflitktfall immer Feature gewinnt

git checkout feature
git merge -s ours master

Git Rebase

Rebase is little different than merge. Rebase apply all feature branch changes on top of master branch by creating new commit for each of its previous commit messages. Which means that rebase command will change your commit history and regenerate commits again on top of master branch. Final output of git rebase can be represent as follows.

Rebasing the feature branch onto master

Following command can be used to rebase feature branch on top of master branch. “-i” option used for interactive rebase. Otherwise you can simply use “git rebase master”.

git checkout feature
git rebase -i master

The major benefit of rebasing is that you get a much cleaner project history:

  1. First, it eliminates the unnecessary merge commits required by git merge
  2. Second, as you can see in the above diagram, rebasing also results in a perfectly linear project history — you can follow the tip of feature all the way to the beginning of the project without any forks

This makes it easier to navigate your project with commands like git loggit bisect, and gitk.

Rebase or Merge

You can use either rebase or merge to combine two branches. Peoples are tend to use rebase over merge due to following facts.

  • There is no additional commit added to the merged branches
  • Rebase commit history is cleaner than merge history since rebase history does not have complex branches
  • Due to the branch complexity in merged git tree, some code changes are invisible. But rebase changes come from a specific and entitled commit.
  • Rebase make easier to use git log, git bisect, and gitk commands.

When to not to use Rebase

As we already discussed, rebase change the commit history. Therefore, it should not be apply on the public branches(eg: master) where other people are working on. This make git confuse that your master branch diverge from others master branch. So, before you run git rebase, always ask yourself, “Is anyone else looking at this branch?”. If the answer is yes, take your hands off the keyboard and start thinking about a non-destructive way to make your changes. Otherwise, you’re safe to re-write history as much as you like.

Conclusion

Both merge and rebase can be used to combine two branches. Merge command just unify your work with a commit without changing history. While rebase apply feature branch changes on top of master branch and change the history. If you prefer to have clean history, then you can use rebase. If you need to preserve the history changes, then merge would be the best choice.

References:

https://www.atlassian.com/de/git/tutorials/merging-vs-rebasing

https://dev.to/dhanushkadev/

By Shabazz

Software Engineer, MCSD, Web developer & Angular specialist

Leave a Reply

Your email address will not be published. Required fields are marked *