Governança de Nuvem, FinOps e Liderança Técnica

Differences Between Git Merge and Git Rebase

Understanding the difference between them is essential to avoid unnecessary conflicts, lost productivity, and even problems in the project's history.

codeconsistencygit-strategies

Git Merge vs Git Rebase: What's the Difference and When to Use Each?

One of the most common questions among developers who start working with Git in teams is:

Should I use Merge or Rebase?

The answer is: both have their place.

Understanding the difference between them is essential to avoid unnecessary conflicts, lost productivity, and even problems in the project's history.


Git Merge

Merge joins two lines of development while preserving the original history of both. Recommended when:

Command:

bash
git merge feature

Git Rebase

Rebase reapplies your branch's commits onto a new base. Recommended when:

Command:

bash
git rebase main

Practical rule

If the branch is only yours:

bash
git rebase main

If other people also use the branch:

bash
git merge main

Avoid rebasing shared branches.


Understanding Git Merge

Imagine the following scenario:

text
main
A --- B --- C

feature
       \
        D --- E

The feature branch was created from commit B.

During development, new commits were added to both branches.

By running:

bash
git checkout main
git merge feature

Git creates a special merge commit:

text
A --- B --- C -------- M
       \             /
        D --- E -----

Commit M connects the two histories.


Advantages of Merge


Disadvantages of Merge

In large projects it can produce a history that looks like:

text
Merge branch feature-login
Merge branch feature-payment
Merge branch feature-report
Merge branch hotfix

The history becomes more cluttered.


Understanding Git Rebase

Same initial situation:

text
main
A --- B --- C

feature
       \
        D --- E

Running:

bash
git checkout feature
git rebase main

Git does something different. It takes commits D and E and recreates them on top of commit C.

Result:

text
main
A --- B --- C

feature
               \
                D' --- E'

Notice that:

These are new commits. After that:

bash
git checkout main
git merge feature

Result:

text
A --- B --- C --- D' --- E'

No merge commit. Fully linear history.


Advantages of Rebase


Disadvantages of Rebase


Merge vs Rebase in Practice

ScenarioRecommendation
Personal branchRebase
Pull Request before submittingRebase
Shared branchMerge
Large teamMerge
Linear historyRebase
Maximum safetyMerge

The Biggest Mistake With Rebase

Imagine:

bash
git push origin feature

Other developers pull your branch.

Then you run:

bash
git rebase main
git push --force

Now the history has changed. Anyone who already had the branch locally will see:

That's why there's a widely followed rule:

Never rebase commits that have already been shared with other people.


Conclusion

Merge and Rebase don't compete with each other. They solve different problems.

A strategy widely used by modern teams is:

  1. Develop on the feature branch.
  2. Rebase onto main before the Pull Request.
  3. After approval, merge into main.

This way, it's possible to get a clean history without giving up safety during collaborative development.