What is the difference between git merge and git rebase?
Merge and rebase are two ways to combine changes from one branch into another, but they work differently.
git merge creates a new merge commit that combines the histories of both branches. History is preserved fully — you can see when each branch diverged and merged. This is safe for shared branches because it doesn't rewrite history. The downside is that history can become cluttered with many merge commits, especially in large teams. Use merge when combining a feature branch into main or when working with public repositories.
git rebase moves your commits on top of the target branch, creating a linear history. Git takes your commits, undoes them, updates the branch to the target state, and re-applies your commits. History looks clean and straight, without merge commits. The downside is that rebase rewrites history, which is dangerous for shared branches: if someone has already pulled your branch, after rebase their history won't match yours. Use rebase for local feature branches before merging into main.
The golden rule: never rebase public branches (main, develop, or branches that other developers reference). If a branch is already pushed to remote and someone might be working on it — use merge.
Many teams use a hybrid approach: rebase the feature branch before merge, then merge into main with --no-ff (no fast-forward) to preserve the merge point. This gives a clean commit history while still showing which commits belonged to the feature branch.