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
Solutions Architect at Corgineering.com
โซ ๐๐๐ซ๐ ๐
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!
This article is part of our Best Practices series. Check out our other articles.