What you'll learn
Quick Answer
Both git merge and git rebase combine changes from one branch into another — the difference is what they do to your history. Merge creates a new merge commit and keeps every branch and commit exactly as it happened, so the history is complete but can look tangled. Rebase moves your commits on top of the other branch and replays them as brand-new commits, giving you a clean, straight line but rewriting commit hashes. Use merge for shared branches, and rebase to tidy up your own local work before sharing it.
The quick picture
Sooner or later, every Git user hits the same fork in the road: you finished some work on a branch, the main branch has moved on, and now you need to bring the two together. Git gives you two ways to do this — git merge and git rebase — and the choice confuses almost everyone at first.
The good news is that git merge vs rebase is not really a fight about which command is "better." They both combine work from two branches. The real difference is what they do to your commit history — the record of who changed what and when. Merge preserves the full story exactly as it happened. Rebase rewrites it into a tidy, straight line.
In this guide we'll look at exactly what each command does, what your history looks like afterward, when to reach for each one, and the single rule you must never break. If branches still feel shaky, our free Git course walks through them from scratch.
What git merge does
A merge takes the changes from one branch and joins them into another. Say you branched off main to build a feature, and meanwhile a teammate pushed new commits to main. To pull their work into your branch, you run:
git checkout feature
git merge mainIf both branches have moved on, Git creates a brand-new commit called a merge commit that ties the two histories together. Nothing gets rewritten — your commits keep their original hashes, and the merge commit simply records that a join happened here.
Before the merge, the history looks like this (you branched off, then both sides added commits):
A---B---C feature
/
D---E---F---G mainAfter git merge main into feature, Git adds a merge commit M that has two parents:
A---B---C---M feature
/ /
D---E---F-------G mainBoth branches are still visible in the picture. Run git log --oneline --graph --all and you'll see the same shape in your terminal. That's the whole idea of merge: it stays honest about how the work actually happened.
One nuance: if main hasn't moved at all since you branched, Git does a fast-forward merge — it just slides the branch pointer forward and creates no merge commit. A merge commit only appears when both branches have new commits of their own.
What git rebase does
A rebase answers a different question: "What if my work had started from the latest main in the first place?" Instead of joining branches with a merge commit, rebase moves your commits so they sit on top of the other branch.
git checkout feature
git rebase mainGit takes each of your commits (A, B, C), sets them aside, fast-forwards your branch to the tip of main, and then replays your commits one by one on top. Starting from the same split as before:
A---B---C feature
/
D---E---F---G mainAfter git rebase main, the history becomes one straight line:
D---E---F---G---A'---B'---C' featureNotice the tick marks: A', B', and C' are new commits with new hashes. They carry the same changes as A, B, and C, but because Git rebuilt them on a new base, their IDs changed and the originals are quietly abandoned. This is what people mean when they say rebase rewrites history — and it's exactly why rebase can be dangerous, as we'll see below.
Rebasing can hit conflicts too, and because commits are replayed one at a time, you may resolve them commit by commit. Fix each one, run git rebase --continue, and if it all goes sideways, git rebase --abort returns your branch to exactly how it was before you started. The payoff is a clean, linear history with no merge commits cluttering the log — which is why many teams love it.
Merge vs rebase: side by side
Here's the whole thing at a glance:
| Behaviour | git merge | git rebase |
|---|---|---|
| Creates a merge commit | Yes | No |
| Keeps original commit hashes | Yes | No |
| Produces a linear history | No | Yes |
| Preserves exact branch topology | Yes | No |
| Safe on shared / pushed branches | Yes | No |
| Easy log to read for beginners | Partial | Yes |
| Simple to undo | Yes | Partial |
Neither column is all green — and that's the point. You pick based on the situation, not out of habit.
When should you use merge?
Reach for git merge when an accurate history matters more than a tidy one:
- Landing a finished feature into
main. A merge commit is a clear marker that "this feature arrived here," which is genuinely useful when you look back months later. - The branch is shared. If other people have already pulled the branch, merging never rewrites their commits, so nobody's local copy breaks.
- You're new to Git. Merge is forgiving. Conflicts show up in one place, you fix them once, and you're done. If something feels wrong,
git merge --abortputs you right back where you started.
The trade-off is that a busy repo full of merge commits can make git log look like a plate of noodles. That's cosmetic, not harmful — nothing is lost.
When should you use rebase?
Reach for git rebase when you want a clean, linear story — and you're only rewriting work that still lives on your own machine:
- Updating your feature branch with the latest
main. Rebasing keeps your commits on top so your branch stays current without piling up merge commits. - Tidying up before a pull request.
git rebase -i main(interactive rebase) lets you squash "fix typo" commits together, reorder them, and reword messages so reviewers see one clean set of changes. - Projects that require a linear history. Some teams ban merge commits entirely and rebase every branch before it lands.
A common everyday version is git pull --rebase, which fetches new commits and replays yours on top of them, instead of creating a fresh merge commit every time you pull.
The golden rule of rebasing
Never rebase commits that you have already pushed and shared with others.
This is the one rule that saves you from real pain. Because rebase replaces commits with new-hash copies, rebasing a branch that teammates have already pulled means their history and yours no longer match. When they try to pull or push, Git gets confused, duplicate commits appear, and someone ends up untangling the mess by hand.
The safe boundary is simple:
- Local, private, not-yet-pushed commits: rebase freely.
- Pushed, shared, on a branch others use: merge instead.
If you genuinely must update a pushed branch that only you use — say, after squashing your own pull request — use git push --force-with-lease rather than a plain --force. It refuses to overwrite the branch if someone else pushed in the meantime, a safety net that a bare force push does not give you.
Our recommendation
Here's the workflow we teach beginners at Priodemy, and it keeps you out of trouble:
- While building on your own branch, run
git pull --rebaseorgit rebase mainto stay current — it's clean, linear, and entirely local. - Before opening a pull request, optionally run
git rebase -ito tidy your commits into a story reviewers can follow. - When the feature is ready, merge it into
main(or let your PR do the merge). This leaves a clear landing point and never rewrites shared history.
In short: rebase to tidy your own work, merge to combine shared work. Learn both, respect the golden rule, and Git stops being scary. For the commands you'll reach for every day, keep our Git commands cheat sheet open in a tab, and work through the branching lessons in the free Priodemy Git course to practise all of this hands-on.
Frequently Asked Questions
Does git rebase delete my commits?
No. Rebase replays your commits as new copies with new hashes and abandons the originals, but the old commits still exist in Git's reflog for a while. If a rebase goes wrong, git reflog plus git reset --hard can bring your branch back to how it was before.
Is git rebase dangerous?
Only when you rebase commits that other people already have. Rewriting shared history is what causes duplicate commits and broken pulls. Rebasing your own local, unpushed commits is completely safe and is a normal daily habit.
What does git pull --rebase do?
It fetches new commits from the remote and then replays your local commits on top of them, instead of creating a merge commit. The result is a cleaner, linear history every time you pull. It's a safe default because it only rewrites your own unpushed commits.
How do I undo a rebase?
Run git reflog to find the commit your branch pointed at before the rebase (look for an entry like "HEAD@{1}"), then run git reset --hard HEAD@{1}. If you're mid-rebase and want out, git rebase --abort restores the branch instantly.
Which should beginners learn first, merge or rebase?
Start with merge. It's forgiving, it never rewrites history, and git merge --abort gives you an easy exit. Once you're comfortable with branches and conflicts, add rebase for tidying up your own local work before sharing it.
