Mail merge

Git Rebase versus Git Merge. What to use? | by Kayathiri Mahendrakamaran | May 2022

What to use?

picture by Praveen Thirumurugan in Unsplash

Git Rebase and Git Merge commands are commonly used to merge two branches. There’s a lot of discussion about whether git merge or git rebase is better. We’ll answer all your concerns about Git Rebase and Git Merge in today’s Git Rebase versus Merge blog.

To merge the work of different developers into a single code, use the Git Merge and Git Rebase commands. Although both statements have the same ultimate goal, their implementation differs.

Photo created by the author

What will happen if you start working on a feature branch? Another developer can add the commits to the master branch at the same time. In order to integrate these changes into the feature branch, you have two options to do so: Merge the master branch with the feature branch or rebase the master branch with the feature branch.

Let’s see the first otrion: Merge Git. How can you do this? You need to run a few simple commands.

git checkout feature-branch
git merge master

Moreover, you can achieve this with a single command.

git merge master feature

There is a change for merge conflicts when you merge branches. What are merge conflicts? Suppose one developer is working on one branch and another developer is working on another branch and they are both trying to merge their changes with the master. This is where merge conflicts occur. We can use some tools to resolve these merge conflicts manually. We can choose which edits to keep and which chick edits to delete.

Photo created by the author

This will add a merge commit to your feature branch. But if your master branch is active, it will pollute your feature branch history. If you want to keep your feature branch history clean, you should use rebase ordered.

As an alternative option, you can use git rebase . You can do this with the following commands:

git checkout feature-branch
git rebase master
Photo created by the author

Rebase boost to start the whole feature branch above the main branch. It integrates all changes from the master branch into the feature branch. Simply, it rewrites git history creating new commits for commits in master. The main advantage here is that we can get a clear history from git. It also makes it easier to navigate to any level of validation.

The downside is that you don’t know when major changes are incorporated into the feature branch. This can sometimes be catastrophic.

Think before you do!!!

Think you are working with the public branch. The rule of thumb is not to use it with public repositories. If you try to rebase the master branch on your feature branch, all changes will be made to your local repositories. There will be diverging changes and commits by different developers in the master branch. This will cause additional merge actions which will be very confusing.

If you are working with a small team, it would be beneficial to use git rebase and if you are working with a large team, try using git merge.