Best Practicesยท5 minutes read

Git rebase vs merge vs rerere

Have you ever heard about rerere? Do you know what is the difference between rebase and merge ? Don't worry I will try to answer those questions!

Linas Kapoฤius

Linas Kapoฤius

Solutions Architect at Corgineering.com

November 5, 2024
Git rebase vs merge vs rerere

โšซ ๐Œ๐ž๐ซ๐ ๐ž

When you merge, youโ€™re combining your branchโ€™s changes with the main branch (or another branch). Git does this by creating a new merge commit that links the two branches together. This is like two paths meeting up at a crossroadโ€”you can see where they came from and where they meet.

Pros:

  • Simple and safe, especially in team projects.
  • Keeps the full history of changes.

Cons:

  • Your history can get cluttered with extra merge commits.
  • Frequent merges increase the chance of merge conflicts.

๐Ÿ’กBonus point: Merge commits could be useful target for triggering CI/CD pipelines.

๐Ÿ”ด ๐‘๐ž๐›๐š๐ฌ๐ž

Rebasing, on the other hand, rewrites history. It takes your changes and moves them to the tip of the main branch, as if youโ€™d been working there the whole time. Think of it as editing your timeline so everything looks like it happened in one straight line.

Pros:

  • Creates a clean, linear history with no merge commits.
  • Makes it easier to understand the sequence of changes.

Cons:

  • Risky in shared branches because it rewrites history.
  • If conflicts arise during rebase, you have to fix them at each step.

โšซ ๐‚๐จ๐ง๐Ÿ๐ฅ๐ข๐œ๐ญ๐ฌ

There is misconception that rebase cause fewer conflicts overall. Which is simply ๐ง๐จ๐ญ ๐ญ๐ซ๐ฎ๐ž. The key difference is when you face those conflicts.

  • With ๐ฆ๐ž๐ซ๐ ๐ž, you get conflicts all at once when you merge the branches together. In other words, you are solving all the conflicts at once which could be convenient or overwhelming.
  • With ๐ซ๐ž๐›๐š๐ฌ๐ž, you face conflicts during the rebase process, possibly at each commit, making it feel like conflicts happen "step by step." So by using rebase, conflicts are handled one commit at a time which also could be convenient or overwhelming.

๐Ÿ’ก๐„๐ฑ๐ญ๐ซ๐š ๐Ÿ๐š๐ง๐œ๐ฒ ๐๐จ๐ง๐ฎ๐ฌ ๐“๐ข๐ฉ: Whether you choose rebase or merge, merge conflicts are a fact of life. But hereโ€™s a hack: use git ๐ซ๐ž๐ซ๐ž๐ซ๐ž (reuse recorded resolution) to let Git remember how you resolved past conflicts. Next time the same conflict shows up, Git will handle it automatically!

โšช ๐–๐ก๐ž๐ง ๐ญ๐จ ๐”๐ฌ๐ž ๐–๐ก๐ข๐œ๐ก?

  • Use merge if you want a full history, especially in shared or team projects.
  • Use rebase for a cleaner, simpler historyโ€”just be careful with shared branches to avoid rewriting history others rely on!

    Blog image

This article is part of our Best Practices series. Check out our other articles.