It is 4:47 p.m. on a Thursday. You have finished a feature branch, three teammates have merged their own work into main since lunch, and your terminal is asking a question you have never been sure how to answer: merge or rebase? You pick one at random, a wall of conflict markers appears, and suddenly the afternoon is gone.

If that sounds familiar, you are in good company. Git is one of the few tools developers use every day without ever fully trusting. This guide explains what merge and rebase actually do to your history, when each one is the right call, and the one rule that prevents almost every disaster.

What Git is really storing

Before comparing the two commands, it helps to see what a branch is. A Git history is a chain of commits, and each commit points back to its parent. A branch is nothing more than a movable label pointing at one commit.

When you create feature from main, both labels point at the same commit. As you commit, feature moves forward. When teammates commit to main, it moves forward too. Now the histories have diverged: they share an ancestor but each has commits the other lacks.

Merge and rebase are two different answers to the same question: "How do I get these two lines of work back into one?"

A branch is not a folder of files. It is a sticky note on a commit.

Merge: tie the two histories together

A merge creates a new commit with two parents, one from each line of work. Nothing that already existed is changed.

git switch main
git merge feature

The result is a history shaped like a diamond. It is honest: it records that work happened in parallel and was joined at a specific moment. If something breaks, you can see exactly when the two lines met.

The cost is noise. A busy repository full of merge commits like "Merge branch 'main' into feature" becomes hard to read, because the graph looks like a bowl of spaghetti. That is the main complaint people have, and it is a fair one.

Rebase: replay your work on a new base

A rebase takes the commits unique to your branch, sets them aside, moves your branch to the tip of main, and then replays each commit one at a time on top.

git switch feature
git rebase main

The final history is a single straight line, as if you had started your work this morning from the latest main. Reading git log becomes pleasant, and bisecting a bug becomes much easier.

But there is a subtle detail: replayed commits are new commits. They have the same changes and messages, but different parents, so they get different hashes. Rebase does not move your commits; it rewrites them.

MergeRebase
History shapeBranches and joinsStraight line
Existing commitsUntouchedRewritten (new hashes)
ConflictsResolved oncePossibly once per commit
Safe on shared branchesYesNo
Best forIntegrating finished workTidying your own work

The golden rule: never rewrite shared history

Here is the sentence worth memorizing: do not rebase commits that other people already have.

Imagine you push feature, and a colleague pulls it and builds on top. Then you rebase and force-push. Your commits now have new hashes, but your colleague's work is based on the old ones. Git sees two unrelated versions of the "same" history, and untangling it is a miserable hour for everyone.

Rebasing your own private, unpushed (or only-you-use) branch is perfectly safe. Rewriting anything that others have pulled is not. If you must force-push to your own branch, prefer the safer variant:

git push --force-with-lease

It refuses to overwrite the remote if someone else pushed in the meantime, which is exactly the situation you want to be warned about.

A practical workflow that works for most teams

You do not have to pick a side forever. Many teams use both, each where it shines.

  1. While developing, keep your feature branch fresh by rebasing it onto the latest main: git fetch then git rebase origin/main. Conflicts show up early, in small pieces, while you still remember what you changed.
  2. Before opening a pull request, tidy your commits with an interactive rebase. Squash the "fix typo" and "oops" commits into meaningful ones.
  3. When integrating, let the team's convention decide: a merge commit, a squash merge, or a fast-forward.

The interactive rebase deserves a short look, because it is the most useful trick in this whole article:

git rebase -i HEAD~4

Git opens an editor listing your last four commits. Change pick to squash (or s) on the ones you want folded into the previous commit, or reword to fix a message. Save, and your messy afternoon becomes a clean story.

Handling conflicts without panic

Conflicts feel scary mostly because the markers are ugly. In reality they are just Git saying, "Two people changed the same lines; you decide."

<<<<<<< HEAD
timeout = 30
=======
timeout = 60
>>>>>>> feature

Edit the file to the version you want, remove the markers, then continue:

git add config.ini
git rebase --continue    # or: git commit, if you were merging

If you get lost mid-rebase, there is always a way out: git rebase --abort returns you to exactly where you started. Knowing that escape hatch exists makes the whole process less frightening.

And if you already finished a rebase and regret it, git reflog lists where your branch pointed recently. Find the hash from before the rebase and run git reset --hard <hash>. Git rarely loses work; it just hides it.

Common mistakes to avoid

A few habits cause most of the Git pain people complain about. First, rebasing main itself or any long-lived shared branch: do not. Second, running git pull on a diverged branch without knowing what it will do. By default git pull is a fetch plus a merge, which quietly creates those noisy "Merge branch" commits; git pull --rebase replays your local commits instead, and you can make it the default with git config --global pull.rebase true.

Third, leaving a branch alone for weeks. The longer it drifts from main, the larger and uglier the eventual conflict. A five-minute rebase every morning is far cheaper than a two-hour rescue at the end. Finally, commit small and often on your private branch. Small commits are easy to reorder, squash, or drop during an interactive rebase, so you lose nothing by being messy while you work and tidy afterward.

Which one should you choose?

Use this quick decision guide:

  • Updating your own feature branch with the latest main? Rebase. It keeps your history linear and surfaces conflicts early.
  • Merging a finished feature into a shared branch? Merge (or squash merge), following your team's convention.
  • Branch already pushed and used by others? Merge. Do not rewrite it.
  • Want a perfectly readable log for a release? Rebase before merging, then fast-forward.

Rebase to tidy your own room; merge when you are visiting someone else's.

Summary

Merge preserves history exactly as it happened and is always safe. Rebase rewrites your commits to produce a clean, linear story and is safe only on work that is still yours alone. Remember the golden rule, keep --abort and reflog in your back pocket, and the Thursday-afternoon conflict storm turns into a routine, even slightly satisfying, chore.

Next time your terminal asks that question, you will know the answer. Happy committing.