Benefits of using rebase over merge commits

Matheus Gomes
April 29, 2025

Introduction

When working with Git, one of the common dilemmas developers face is whether to use rebase or merge when integrating changes from one branch to another. While both approaches have their use cases, rebase offers significant advantages in maintaining a clean and meaningful commit history. This blog post will explore why rebase is often the preferred choice for structured Git workflows.

Understanding rebase vs. merge

Merge

A merge operation takes the changes from a feature branch and integrates them into the target branch (e.g., main or develop). This creates a merge commit, which may introduce unnecessary clutter into the commit history.

Example of a merge commit workflow:

This results in a non-linear history with multiple merge commits, which can make it difficult to trace individual changes.

Rebase

A rebase, on the other hand, rewrites the commit history by applying each commit from the feature branch on top of the target branch, resulting in a linear history.

Example of a rebase workflow:

This reapplies each commit in sequence, making the history easier to read and understand.

Key benefits of using rebase

1. Cleaner commit history

Rebasing ensures that the commit history remains linear and organized, avoiding unnecessary merge commits that make it harder to track changes.

2. Improved code review process

A clear, structured history makes it easier for team members to review changes, understand the evolution of a feature, and spot potential issues.

3. Avoiding unnecessary merge conflicts

Frequent rebasing keeps feature branches up to date with the main/develop branch, reducing the likelihood of complex merge conflicts that can arise when a feature branch diverges too much.

4. Better collaboration

When multiple developers are working on a repository, rebasing allows changes to be integrated seamlessly without creating redundant merge commits, making collaboration smoother.

When to use merge instead of rebase

While rebase has many advantages, merge commits are still useful in certain scenarios:

  • Preserving the context of a branch: If a feature branch has multiple contributors, merging keeps the original commit structure intact.
  • Merging long-lived branches: When integrating major branches (e.g., develop into main), a merge commit serves as a historical marker.
  • Avoiding accidental history rewrites: Rebasing rewrites history, which can be problematic for shared branches. Avoid rebasing branches that have already been pushed to a remote repository.

⚠️ When you rebase a branch, Git rewrites the commit history by creating new commits instead of merging. This means that if you've already pushed a branch to a remote repository and then rebase it locally, the commit history on your local machine will be different from the one on the remote. If you then push the rebased branch, Git will require a force push (git push --force), which can overwrite changes made by others who have pulled from the remote.

This is why rebasing is generally discouraged for shared branches (such as main, develop, or any branch actively used by multiple developers). Instead, use merge commits to integrate changes in shared branches while keeping their history intact.

Best practices for using rebase

  • Always rebase feature branches before merging into the upstream branch (e.g. main, develop).
  • Avoid rebasing shared branches to prevent rewriting public history.
  • Use git pull --rebase instead of git pull to avoid unnecessary merge commits when syncing with a remote repository.
  • If conflicts arise, resolve them carefully and continue rebasing using git rebase --continue.
  • When merging a pull request on GitHub, it is preferable to select the "Rebase and merge" option instead of the standard "Merge Pull Request." This way, you will avoid adding a merge commit to the base branch.

Quick tips

1. Git amend

If you need to make a quick commit but don’t want to create a duplicate or come up with a new message, you can use the --amend option of the git commit command. This allows you to modify the last commit message while merging the new changes into it.

First commit:

git commit -sm "Create migration to merge previous migrations for firms app"

Second commit with small changes:

git commit --amend -sm "Create migration to merge previous migrations for firms app"

The -sm option is used to add a “Signed-off” message to your commit, such as:
Signed-off-by: Matheus Gomes matheus.gomes@vinta.com.br

2. Use AI to generate meaningful commit messages

Tools like GitHub Copilot can generate meaningful commit messages based on code changes, helping maintain clarity and consistency in your commit history. Here's an example using VSCode source control page:

3. Squashing commits

If your Pull Request has a lot of duplicate commits or if you rushed to come up with a good commit message, you can squash these changes. You can do this locally using Git commands, as described in this article, or during the Pull Request merge. In the latter case, you should choose the “Squash and Merge” option. GitHub will then open a text area where you can enter a meaningful commit message or modify it as needed. Here's an example:

Conclusion

Rebasing is a powerful tool for maintaining a meaningful Git history, making debugging, code review, and collaboration more efficient. While merge commits have their place, adopting a rebase-first approach can lead to a cleaner and more structured development workflow.

By understanding when and how to use rebase, teams can improve their version control practices and keep their repositories organized for long-term maintainability.