Git Rebase vs. Git Merge: What's the Difference?

March 15, 2023

Introduction

The git rebase and git merge commands are two ways of integrating changes from one Git branch into another. The commands have the same goal to combine the work of multiple developers into one code, but the process for achieving that goal is different.

In this tutorial, you will learn the difference between git rebase and git merge.

Git rebase vs. git merge - what is the difference?

What Is Rebasing?

The git rebase command integrates changes from one branch into a new base branch by replaying all the commits from the old branch into the new branch. The command rewrites the commit history of the original branch and creates a new branch by applying the same set of commits on top of the target branch.

When you rebase a branch, its base changes from one commit to another, making it appear as if it had been created from that commit. However, the branch is composed of completely new commits even though it looks the same.

Use git rebase when you want to incorporate changes from a feature branch into a main branch. For example, use the command when merging the changes from a feature branch into a development or master branch. Another great benefit of git rebase is that it can be used to clean up a messy commit history by combining or reordering commits logically.

The basic syntax for git rebase is:

git rebase <base>

The command rebases the current branch onto the specified <base>, which can be any commit reference (a commit ID, a branch name, a tag, or a relative reference to HEAD).

The diagram below shows a representation of git rebase:

A diagram showing how git rebase works.

Git Rebase Example

Below is an example of how to use git rebase. Follow these steps:

1. On the master branch, create a new branch using git checkout:

git checkout -b new-branch
Creating a new branch in Git.

2. Create some files and add them to the tracking index:

git add .

3. Commit the changes:

git commit -m "Added a new feature"
Creating a commit in Git.

4. Push the new branch to the remote repository:

git push origin new-branch
Pushing a new branch to Git remote repository.

4. Switch back to the master branch:

git checkout master

5. Execute git pull to make sure you have the latest changes from the remote repository:

git pull

6. Rebase the changes from the new branch onto the master branch:

git rebase new-branch

Note: Run git rebase -i to enter an interactive rebasing session. Interactive rebasing allows you to alter individual commits in the process instead of automatically moving all the commits to the new base.

Example of rebasing a branch in Git.

If any conflicts arise during the rebase process, resolve them manually. After resolving the conflicts, run the following command to continue the rebasing process:

git rebase --continue

7. After the rebasing completes, push the changes to the remote repository:

git push origin master
Pushing changes from local repository to a remote one.

The changes are then visible to other developers in the team. However, the commit history has been rewritten which looks like the changes were made directly on the master branch.

Rebasing Advantages and Disadvantages

The git rebase command has certain benefits and drawbacks, which are explained below.

Advantages

  • Linear project history. The major benefit of Git rebasing is a clean project history since the command eliminates unnecessary merge commits. The result is a perfectly linear project history, without any forks.
  • Simplified codebase. The linear history makes it very easy to understand the codebase and track down the origin of specific changes.
  • Resolving merge conflicts. The git rebase command applies changes from one branch on top of another. This means that merge conflicts are simplified, and changes are applied in a more orderly manner than in git merge.
  • Separate feature branches. Rebasing can be used to separate feature branches on the master branch. Separating them makes it easier to manage multiple branches and keep them updated with the latest changes in the master branch.
  • Flexibility. git rebase is more flexible than git merge in managing branches and committing changes because it allows users to reorder or modify commits, change commit messages, etc.

Disadvantages

  • Possible merge conflicts. Rebasing a workflow may cause more frequent merge conflicts if there is a long-lived branch that has strayed far from the master branch. If the branch contains many new commits, they may conflict with the master branch. To avoid such issues, rebase your branches frequently against the master branch.
  • Lost commits. Running git rebase in interactive mode with subcommands that remove commits from the branch can cause lost commits in the branch's immediate log. However, the commits can usually be restored by undoing the rebase with git reflog.
  • Lack of commit info. After rebasing, you cannot see when the upstream changes were made and when they were incorporated into the feature branch.

Note: Learn how to combine multiple commits using one of the git squash methods.

What Is Merging?

The git merge command allows users to merge changes from two different branches into one, usually the master branch. The command uses two commit pointers, the branch tips, and finds a common base commit between them. Then, Git creates a new merge commit that combines the changes.

Merging is useful when multiple developers have been working on the same codebase and have created multiple branches to implement different features or bug fixes. When the developers complete their work, they can merge their changes back into the master branch. Merging is also used when you want to create a new feature branch that includes changes from other branches.

The basic syntax for git merge is:

git merge <branch>

The command merges the changes from the specified <branch> into the branch you are currently on. Hence, git merge is often used with git checkout to switch between branches. If any conflicts arise during the merge, Git prompts you to resolve them before completing the merge.

The diagram below shows a visual representation of git merge:

A diagram showing how Git merge works.

Git Merge Example

The example below shows how to use git merge. Follow the steps:

1. Create a new feature branch from the master branch. For example:

git checkout -b feature-branch
Switching to a new branch in Git.

2. Create files on the new branch and make some changes.

3. Add files to the tracking index, commit the changes, and push the branch to the remote repository:

git add .
git commit -m "New feature"
git push origin feature-branch
Pushing changes from local repo to remote.

4. Checkout back to the master branch:

git checkout master
Switching to the master branch in Git.

5. Synchronize the local repository with the remote one to make sure you have all the latest changes since the feature branch was created. Enter:

git pull

6. Merge the changes from the feature branch into the master branch:

git merge feature-branch
Merging a feature branch with the master branch.

Important: If any conflicts arise during the merge, resolve them manually and then complete the merge.

7. Commit the changes:

git commit -m "Merged feature-branch into master"

8. Push the changes to the remote repository. Run:

git push origin master

The merge process is now complete, and the changes are visible to other developers on the project.

Merging Advantages and Disadvantages

The advantages and disadvantages of git merge are explained below.

Advantages

  • Non-destructive. Merging is a non-destructive operation in Git since it does not change the existing branches. It only adds an extra commit called merge commit.
  • Change integration. Merging allows users to integrate changes from one branch into another. The integration is useful if multiple developers are working on different features that need to be merged into the master branch.
  • Multiple codebase versions. Merging allows users to keep multiple codebase versions. This is useful if older code versions are necessary or if you need a separate branch for feature testing.
  • Change tracking. Merging allows users to track the changes that have been made to the codebase. Tracking is useful for debugging or audits.
  • Conflict resolution. Merging is a great conflict resolution mechanism that allows users to merge changes that multiple developers implemented on the same file.

Disadvantages

  • Merge conflicts. One of the main disadvantages of git merge is the possibility of getting merge conflicts when making multiple changes to the same file. Sometimes, resolving such conflicts can be time-consuming and difficult.
  • Context loss. When the changes from two branches are merged, some of the context of the changes may be lost. Thus, the codebase history and origin of some changes can be more difficult to trace.
  • Complexity. The codebase complexity increases with the number of branches and merges, which increases maintenance difficulty and complicates the relationships between branches.
  • Dependencies. Merging multiple branches into one can create dependencies between different parts of the codebase. This can further hinder testing and change deployment because changes in one part of the codebase can affect other parts.

Note: Learn how to merge a Git branch into master with our step-by-step guide.

Git Rebase vs. Git Merge: Comparison

The main difference between git rebase and git merge is that git rebase creates a new set of commits applied on top of the target branch, while git merge creates a new merge commit that combines the changes from both branches.

The following table sums up the key differences between git merge and git rebase:

git mergegit rebase
Allows users to merge branches in Git.Allows users to integrate changes from one branch to another.
Merging creates a chain-like branch structure.Rebasing creates a linear branch structure.
Logs include the complete commit merging history.Rebase logs are linear and the history is altered to reflect that.
Combines all the commits on the feature branch as a single commit in the master branch.Rebases all the commits to the target branch and adds the same number of commits.
Easier to use than git rebase.More complex than git merge.
Used for projects where the target branch is supposed to be shared.Usually used for projects where the target branch is private.
Suitable when many people are working on the project simultaneously.Suitable for small workgroups.

Git Rebase vs. Git Merge: When to Use?

Both options cater to different use cases and specific needs. Choose which option to use after revising the differences discussed above. Bear in mind how transparent and accessible you want the commit logs to be before choosing.

Use git merge when you want your team to have access to logs that show where each commit comes from. Additionally, it is the best choice when other developers can see the target and source branches, and when you are working in large groups.

On the other hand, use git rebase when the commit logs are not as important, and no one needs to revise them. Rebasing is usually the best choice when working on branches that cannot be accessed by other developers, or when you are working alone.

Using Rebase and Merge Together

A combination of git rebase and git merge on the same project is also possible.

For example, if you are working on a feature branch and create another branch off of it, you can first merge the two feature branches. This implements the changes from the second feature branch into the first one. After that, you can use git rebase to implement the changes from the feature branches into the main branch.

Such a combination of git merge and git rebase allows you to work on a separate branch without other team members seeing anything before you perform the rebase.

Conclusion

This article explained git rebase and git merge, showed the differences between them, provided an example for both options, and gave advice on how to choose the right option for your project. Check out our detailed guide to learn how git rebase and git merge can be used in a specific use case when you want to merge Master into Branch in Git.

Learn more about Git in our Git beginner's guide, learn about Git stashes, or download a handy Git commands cheat sheet.

Was this article helpful?
YesNo
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
SSH vs. HTTPS for Git: Which One to Use?
February 21, 2023

This article compares and gives advice on SSH and HTTPS - two protocols used to secure the connection to remote repositories.
Read more
How to Use git submodule init
September 28, 2022

The git submodule init command adds submodule entries to the local Git configuration file. This tutorial shows you how to use git submodule init.
Read more
Git Tag: An Overview of the Basic Functions
September 6, 2022

This guide shows an overview of basic functions to perform with Git tags. See how to create, delete, push, replace, or checkout a Git tag.
Read more
How to Restore a Git Repository
August 1, 2022

Deleted or overwritten a repository? No backup? Don't worry just yet, this guide covers some steps to try and restore the repository.
Read more