Merging is not the only way to bring branches together. Rebasing moves your commits so they start from the latest version of another branch, as if you had begun your work today. The result is a clean, straight history that is easier to read and review. Rebase is powerful and slightly dangerous: this lesson explains exactly what it does, how to resolve conflicts during a rebase, and the one rule you must never break.
In simple words
- Rebase: moves your commits so they sit on top of the latest main, giving a straight, tidy history.
- Merge vs rebase: merge keeps the true history with merge commits. Rebase rewrites your commits into a straight line.
- Golden rule: never rebase commits other people already have.
Merge versus rebase
Your feature branch started from main, and then main moved on:
f1 ── f2 feature /c1 ── c2 ── c3 ── c4 mainMerging main into feature adds a merge commit that ties the two lines together. Nothing is rewritten, but history gets a diamond shape, and merging main in repeatedly adds noise.
Rebasing feature onto main replays f1 and f2 on top of c4, creating new commits f1' and f2':
f1' ── f2' feature /c1 ── c2 ── c3 ── c4 mainSame code changes, same end result, but a straight line. When the branch is later merged, it can even fast-forward.
Try it
git config --global user.name "Leela Kumar"git clone https://gitlab.com/acme/shop.gitcd shopgit switch -c feature/discountprintf 'def discount(total, pct):\n return round(total * (1 - pct / 100), 2)\n' > discount.pygit add discount.pygit commit -m "Add discount helper"printf '\n\nMEMBER_DISCOUNT = 10\n' >> discount.pygit commit -am "Add member discount constant"Meanwhile, main gets a fix:
git switch mainprintf 'TAX_RATE = 0.18\n\n\ndef total(subtotal):\n return round(subtotal * (1 + TAX_RATE), 2)\n' > pricing.pygit commit -am "Fix GST rate back to 18%"git log --oneline --graph --all -n 4* 5e2b7a9 (HEAD -> main) Fix GST rate back to 18%| * 9c1d4f3 (feature/discount) Add member discount constant| * 7a8e2b0 Add discount helper|/* ce797e5 (origin/main) Add CI pipelineNow rebase the feature branch onto the new main:
git switch feature/discountgit rebase maingit log --oneline --graph --all -n 4Successfully rebased and updated refs/heads/feature/discount.* 3f6a1c8 (HEAD -> feature/discount) Add member discount constant* 2d9e0b4 Add discount helper* 5e2b7a9 (main) Fix GST rate back to 18%* ce797e5 (origin/main) Add CI pipelineThe feature commits now sit on top of the fix, in a straight line. Note their new hashes: rebase does not move commits, it creates new copies and moves the branch to them. The old commits still exist for a while (the reflog can find them) but are no longer on any branch.
What happens inside a rebase
- Git finds the commits on your branch that are not on
main(f1,f2). - It sets them aside and moves your branch to the tip of
main. - It replays each commit one at a time, applying its change and creating a new commit.
- If a replayed change conflicts, it stops and lets you resolve that commit.
Resolving conflicts during a rebase
Conflicts in a rebase are resolved one commit at a time:
git rebase main# CONFLICT (content): Merge conflict in pricing.py# error: could not apply 7a8e2b0... Add discount helper git status # shows the conflicted files# edit the files, remove the conflict markersgit add pricing.pygit rebase --continue # go on to the next commit git rebase --skip # drop this commit and continue (rarely what you want)git rebase --abort # give up and return to exactly where you startedNote that during a rebase, "ours" and "theirs" feel reversed: HEAD is the branch you are rebasing onto (main), and the incoming change is your commit being replayed. Read the conflict content, not the labels.
If the same conflict keeps appearing on every rebase, enable rerere (git config --global rerere.enabled true) so Git remembers your resolution.
Pull with rebase
git pull by default merges the server's changes into your branch, creating "Merge branch 'main' of gitlab.com…" commits whenever you and a colleague both committed. git pull --rebase replays your local commits on top of what you fetched instead:
git pull --rebasegit config --global pull.rebase true # make it the defaultThis is one of the most valuable settings for keeping shared history readable.
Keeping a feature branch up to date
On a long-running branch, rebase onto main regularly so conflicts stay small:
git fetchgit rebase origin/maingit push --force-with-lease # your branch was rewritten, so a normal push is rejected--force-with-lease overwrites the remote branch only if it is still where you last saw it, so you never wipe out a commit a colleague pushed to your branch in the meantime. Plain --force has no such check.
The golden rule
Never rebase commits that others have already based work on.
Rebasing rewrites commits. If a shared branch like main is rebased and force-pushed, everyone who pulled the old commits now has history that no longer matches the server, and their next pull creates duplicates and confusing conflicts.
In practice:
- Rebase freely: your local commits, and your own feature branch before (or during) review.
- Never rebase:
main,release/*, or any branch others commit to. - Merge when integrating into shared branches (that is what merge requests do).
Merge or rebase: which should my team use?
| Merge | Rebase | |
|---|---|---|
| History | true record, with merge commits | linear and tidy |
| Safety | never rewrites | rewrites your branch |
| Conflict resolution | once, in the merge commit | per commit being replayed |
| Typical use | integrating into main (merge requests) | updating your feature branch, pull --rebase |
Many teams combine them: rebase feature branches onto main to keep them current and clean, then merge them through a merge request (often with "squash commits" or fast-forward merges enabled in GitLab).
Real-world scenario: an unreadable history
A team's main history was a tangle of "Merge branch 'main' into feature/x" and "Merge remote-tracking branch 'origin/main'" commits; finding when a change arrived meant following lines across a dozen parallel branches. They made two changes: everyone set pull.rebase true, and the GitLab project switched to "Merge commit with semi-linear history", which requires a branch to be rebased on main before it can be merged. git log --oneline became a readable story again, and git bisect got much faster because it no longer had to wade through merge commits.
Common mistakes
- Rebasing a shared branch and force-pushing it.
- Using
git push --forceinstead of--force-with-leaseafter rebasing your branch. - Panicking mid-rebase:
git rebase --abortreturns you to the starting point;git reflogcan recover anything afterwards. - Forgetting to run tests after a rebase: replayed commits can combine into broken code even without conflicts.
- Rebasing when you meant to merge into
main: integrate into shared branches with merges or merge requests.
Practice
- Create a feature branch with two commits, add a commit to
main, and rebase the feature branch ontomain. Comparegit log --oneline --graph --allbefore and after. - Notice that the rebased commits have new hashes.
- Create a conflicting change on both branches, rebase, resolve the conflict and
git rebase --continue. - Start another conflicting rebase and cancel it with
git rebase --abort. - Set
pull.rebase truein your global config.
Cheat sheet
git rebase main replay current branch onto maingit rebase --continue after resolving a conflict (git add first)git rebase --skip drop the current commitgit rebase --abort go back to before the rebasegit pull --rebase fetch and rebase local commitsgit config --global pull.rebase truegit push --force-with-lease update your rewritten branch safelygit config --global rerere.enabled trueKey takeaways
- Rebase replays your commits on top of another branch, giving a straight history; it creates new commits with new hashes.
- Conflicts are resolved commit by commit, then
git rebase --continue;--abortalways gets you out. git pull --rebasekeeps shared history free of needless merge commits.- After rebasing your own branch, push with
--force-with-lease. - Golden rule: never rebase commits others already have.