além do script
Git

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

by Marcelo Macedo

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:

  • The branch is shared with other people.
  • You want to keep the real history of the project.
  • The team prioritizes safety over a perfectly linear history.

Command:

bash
git merge feature

Git Rebase

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

  • You're working alone on the branch.
  • You want a cleaner, linear history.
  • You're about to open a Pull Request.

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

  • Doesn't alter existing history.
  • Safer for teams.
  • Easy to understand.
  • Avoids rewriting commits.

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:

  • D became D'
  • E became E'

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

  • Cleaner history.
  • Makes project analysis easier.
  • Excellent for Pull Requests.

Disadvantages of Rebase

  • Rewrites history.
  • Can cause confusion in teams.
  • May require a force push.

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:

  • Duplicate commits
  • Unexpected conflicts
  • An apparently broken history

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.

  • Merge prioritizes safety and history preservation.
  • Rebase prioritizes cleanliness and linearity of the history.

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.