Git merge vs Git rebase: A beginner's guide to Git branching and merging

Git merge vs Git rebase: A beginner's guide to Git branching and merging

git merge and git rebase are two different ways of combining the changes from multiple branches in Git. Before knowing the difference between git merge and git rebase, we need to understand what is git merge and git rebase.

Git Merge

git merge is a command used to integrate changes made in one branch into another? When you merge a branch, Git creates a new commit that records the fact that the changes have been merged. The new commit is added to the branch that you are merging into, and the branch that you are merging from is left unchanged.

Imagine that we are working on a project with a main branch and a feature branch, and we have made several commits on the feature branch that introduce new functionality. We are now ready to merge the feature branch into the main branch.

To do this, we can use the git merge command to combine the changes from the feature branch into the main branch. For example, we can use the following command:

git checkout main
git merge feature

This will apply the changes from the feature branch to the main branch, creating a new commit that represents the combination of the two branches. If there are any conflicts between the feature branch and the main branch, Git will stop the merge process and ask us to resolve the conflicts. Once the conflicts are resolved, we can use the git commit command to commit the resolved changes and complete the merge.

There are two main types of merge: fast-forward merge and three-way merge. A fast-forward merge is used when the branch you are merging from is a direct ancestor of the branch you are merging into. In this case, Git simply moves the branch pointer to the commit that you are merging from. A three-way merge is used when the branch you are merging from has diverged from the branch you are merging into. In this case, Git creates a new commit that combines the changes from both branches.

Advantage Git Merge

  • Preserves the linear history of the project
  • Easy to understand and use

Disadvantages of Git Merge

  • It can lead to large and complex merge conflicts
  • It can result in a cluttered commit history

GIT Rebase

git rebase is a command that allows us to take the changes we made on one branch and apply them on top of another branch. This is useful when we want to keep our branch's history clean and linear.

Let's say we are working on a new feature on a branch called "feature" and in the meantime, other team members have added new commits to the main branch. When we want to get the latest updates from the main branch and keep out feature branch's history clean, we can use git rebase to apply our feature branch's commits on top of the main branch. This way, it will look like we were working on the latest version of the main branch, instead of an older version.

To do this, we can use the git rebase with using command:

git checkout feature
git rebase main

Things to keep in mind when using git rebase

  • git rebase is a way to take the changes we made on one branch and apply them on top of another branch. But unlike git merge, it doesn't keep a record of the original branch's history. This means that it's not possible to merge the changes back into the original branch.

  • Git rebase is a local operation. It only affects the local repository, so we need to push the changes to the remote repository after we're done.

  • git rebase is a destructive operation, and rebase doesn't keep a record of the original branch's history. This means that it's not possible to revert the changes after they have been rebased. If we want to undo the changes, we need to use git reflog to find the commit before the rebase and reset the branch to that commit.

Advantages of Git Rebase

  • It keeps a clean and linear commit history.

  • It helps avoid merge conflicts.

  • It allows us to squash commits or reorder them.

Disadvantages of Git Rebase

  • It can be more difficult to understand and use.

  • It can lead to lost commits if not used carefully.

  • It can alter the commit history which can be confusing for other developers.

Difference between git rebase and git merge

Git rebase and merge are two different ways of combining the changes from multiple branches in Git. Here is a summary of the main differences between the two:

  • Purpose: The main purpose of git rebase is to clean up the commit history of a branch by reapplying commits on top of a different base. This can be useful for creating a linear history or for merging branches in a more streamlined way. The main purpose of git merge is to combine the changes from multiple branches into a single branch.

  • Commit history: When we use git rebase, the commit history of the branch being rebased is rewritten, and the rebased commits are added on top of the target branch. This can result in a linear commit history with fewer merge commits. When you use git merge, a new commit is created that represents the combination of the two branches, and the commit history of both branches is preserved. This can result in a commit history with more merge commits.

  • Conflicts: If there are conflicts between the branches being rebased or merged, both git rebase and git merge will stop the process and ask you to resolve the conflicts. However, the way conflicts are resolved can be different in the two cases. With git rebase, conflicts must be resolved one commit at a time, while with git merge all conflicts can be resolved at once.

  • Branches: git rebase can be used to rewrite the commit history of a single branch, while git merge can be used to combine the changes from multiple branches into a single branch.

Lets take a look at the difference between git rebase and git merge with an example. Here is an image of the commit history of the main branch and a feature branch:

Cors explained Cors explained
main branch commit history feature branch commit history
Main Branch Commit history post rebase vs merge
Cors explained Cors explained
Git Merge Git Rebase

Here we can notice:

  • In case of git merge, there is a merge commit, while in the case of git rebase there is no merge commit.

  • In case of git merge, commit order is the same as in the original branch, while in the case of git rebase, commit order is different from the original branch.(timestamp of commit)

Lets us understand this with a simple flow diagram: In image-5 there is a feature branch that is created from the main branch, and there is some commit in the feature branch as well as in the main branch. Now we will do git merge and git rebase with the main branch, and see the difference in commit history.

Image-5
Image-5

We can notice that there is no change in git hash and neither in commit order in the case of git merge. But in the case of, the git hash of main(branch being rebased) and commit order is changed.

When to use git rebase and when to use git merge

It is typically recommended that git rebase should be used only on local development and git merge should be used for merging branches that are pushed to remote repositories. But there are some cases where git rebase is preferred over git merge. Here are some of them:

  • When we want to clean up the commit history of a branch by reapplying commits on top of a different base.
  • When we want to combine the changes from multiple branches into a single branch, but don't want to create a merge commit. This can be useful for creating a linear history.

Summary

In summary, both git merge and git rebase can cause conflicts, but they differ in how they handle and preserve the branch's history, and how they present the changes in the branch. Both commands serve the same purpose, but they have different implications on the git history. Merging creates new commits and makes the history more complex while rebasing can make the history linear but it can cause conflicts and make it harder to resolve them.

If you like this article, don't forget to share it with your friends and colleagues.