Updated
Both git merge and git rebase take changes from one branch and integrate them into another. They differ in what history looks like afterward.
Merge: keep history as it happened
git switch feature/search
git merge main
This creates a merge commit that ties the two lines of history together. It’s honest and safe, but frequent “Merge branch ‘main’ into …” commits can clutter the log.
Rebase: replay your commits on top
git switch feature/search
git rebase main
Git sets your commits aside, moves the branch to the tip of main, and replays them one by one. The result is a straight line, as if you’d started your work today.
The catch: the replayed commits are new commits with new hashes.
The golden rule
Never rebase commits that other people already have.
If you’ve pushed a branch that others pulled, rebasing it rewrites history underneath them. For your own feature branch it’s fine. You’ll then need to force-push, and you should do it safely:
git push --force-with-lease
--force-with-lease refuses to overwrite the remote if someone else pushed in the meantime.
Interactive rebase: tidy before you share
git rebase -i main
You get an editor listing your commits. Change pick to:
squash/fixup: fold a commit into the one abovereword: edit the messagedrop: remove it- or reorder the lines
Which should you use?
| Situation | Use |
|---|---|
Updating your feature branch with the latest main |
rebase |
| Cleaning up commits before opening a PR | rebase -i |
| Integrating a finished feature into a shared branch | merge (often --no-ff) or a squash-merge PR |
| Anything already pushed and shared | merge |
Gitflow traditionally uses merge --no-ff for features, releases and hotfixes. Many teams also rebase feature branches locally before merging.
One Git tip a week
Short, practical, no spam. Get the free printable Gitflow cheat sheet when you join.
Newsletter launching soon.