Your feature branch started from the main branch on Monday. By Thursday, three colleagues have pushed to main, and you need their changes before you can finish. Git offers two ways to bring them in. Both end with your branch containing everyone’s work. They differ in what the history says afterwards, and that difference is the whole argument.
What merge does
A merge takes the tip of main and the tip of your branch and creates a new commit with two parents, joining the two lines of work. Your original commits are untouched; their hashes, their dates and their order remain exactly as you made them. The history becomes a graph with forks and joins.
Merge is honest. It records that you worked in parallel and that, at a particular moment, someone combined the two. If a conflict arose, it was resolved once, in the merge commit, and you can see that resolution later. Nothing is rewritten, so it is safe to do on a branch other people have already pulled.
The cost is noise. If you pull main into your branch every day, your history fills with “Merge branch main into feature” commits that carry no meaning of their own. Reading the log, or tracing a bug through it, gets harder.
What rebase does
A rebase takes your commits, sets them aside, moves your branch’s starting point to the latest main, and then replays your commits one by one on top. Each replayed commit is a new commit with a new hash, even if its content looks the same. The result is a straight line: main’s history, then yours.
The log then reads like a story told in order, and a later fast-forward merge adds no extra commit. Interactive rebase also lets you tidy before sharing: squash “fix typo” and “oops” commits into the one they belong to, reword vague messages, drop experiments.
The cost is that you rewrote history. If conflicts occur, you may resolve them several times, once for each replayed commit that touches the conflicting lines. And the rewrite has a hard rule attached.
The rule that decides most cases
Do not rebase commits that other people already have. Suppose you pushed your branch and a teammate pulled it to review or build on. You then rebase and force-push. Your commits now have new hashes; the teammate’s copy still holds the old ones. Their next pull sees two versions of the same work, and they end up untangling duplicates or conflicts that you caused. Rebase what is private to you; merge what is shared.
If you must force-push your own branch after a rebase, use the lease variant, which refuses to overwrite the remote if someone else has pushed there in the meantime. Plain force-push can silently destroy a colleague’s commits.
The side I take
My view, which not everyone shares: use rebase on your own local, unpublished work to keep it current and tidy, and use a merge (often as a squash merge through the pull request) to land the branch on main. That gives you the good parts of both. Your private mess is cleaned up before anyone sees it, and shared history is never rewritten.
Why not rebase everything, as the purists suggest? Because a team is rarely made of people who all understand what a rewrite does. One person force-pushing to a shared branch on a busy Friday does more damage than a hundred merge commits ever will. A rule that depends on everyone remembering a subtle exception is a weak rule.
Why not merge everything, as the other camp says? For a long-lived feature branch, daily merges from main produce a log that is hard to bisect. A short branch rebased once before review is far easier to read.
Squash, the third option your platform offers
GitHub, GitLab and Bitbucket let you merge a pull request in three ways: a regular merge commit, a squash (all the branch’s commits collapse into one on main), or a rebase-and-merge (commits replayed onto main, no merge commit). Squash gives a main history with one commit per pull request, which is easy to scan and easy to revert as a unit. You lose the detailed step-by-step of the branch, so the pull request’s description has to carry the explanation. Check which options your team’s repository permits and agree on one, since mixed styles make the log confusing.
When each one hurts
- Release or hotfix branches that must preserve exactly what shipped usually want merges, because auditors and incident reviews care about the real sequence.
- A branch with fifty commits and many conflicts is painful to rebase; merging once is kinder, then squash at the end.
- Reverting a feature is simpler with a single squashed commit or a clear merge commit than with a scattered series.
If you rebase by mistake and lose commits, they are rarely gone. The reflog keeps a record of where your branch pointed, usually for a few weeks by default, and you can reset back to it. Knowing that makes the whole subject less frightening, and it is worth learning before you need it.
Leave a Reply