What is the difference between git merge, rebase and squash?
BeginnerConceptBranching, versioning & releases
Answer
In one line: All three bring changes from one branch into another: merge keeps the full history with a merge commit, rebase rewrites your commits on top of the target for a straight history, and squash combines all your commits into one.
Merge
git checkout main
git merge feature/login
- Creates a merge commit joining the two histories.
- Keeps every original commit exactly as it was.
- History can look messy with many branches, but it's honest and safe.
Rebase
git checkout feature/login
git rebase main
- Replays your commits on top of the latest main, as if you started from there.
- Gives a clean, linear history.
- Rewrites commit IDs — never rebase commits others are already using (shared branches), or you'll cause problems for them.
Squash (merge)
- All commits of the PR become one commit on main (for example GitHub's "Squash and merge").
- Main gets one tidy commit per feature: "Add login page (#142)".
- You lose the individual "fix typo" and "WIP" commits — usually a good thing.
| Merge | Rebase | Squash | |
|---|---|---|---|
| History | Full, with merge commits | Linear, rewritten | One commit per PR |
| Rewrites commits | No | Yes | Yes (new single commit) |
| Best for | Preserving exact history | Updating your own branch | Clean main branch |
A common team setup: rebase your own feature branch on main to keep it updated, then squash-merge the PR. Main stays clean, and each commit maps to one PR — which also makes git revert of a feature simple.
Tip for interviews: the golden rule: "don't rebase public/shared branches."
More on Branching, versioning & releases
What is a branching strategy, and why does it matter?BeginnerWhat is trunk-based development?BeginnerWhat is GitFlow, and when would you use it?BeginnerWhat is semantic versioning?BeginnerWhat is a Git tag, and how are tags used in releases?BeginnerWhat is a pull request, and what makes a good code review?Beginner