Video in progress
When do you choose merge, and when rebase?
Rebase to update my own branch that nobody else has used yet, and to keep the history clean. Merge for combining shared branches, and wherever rewriting history is risky. The main rule: do not rebase commits that others have pulled.
📖 Answer
Both lead to the same resulting code, and the difference is the shape of the history.
Merge preserves the real history: when the branch split off, when it was merged. The downside is that with many parallel branches, git log --graph turns into a "railway station".
Rebase rewrites commits on top of another base. The history is a straight line, and git bisect and review are easier. But the commit hashes change.
This is how I work in practice:
# update my feature branch with main
git fetch origin
git rebase origin/main
git push --force-with-lease
# merge the feature into main through a PR (merge or squash)
Where not to rebase: main, develop, release branches, and a feature branch two people work on together. Otherwise, when your colleague does a pull, the history splits in two and the same change appears twice.
On a long-lived branch, a rebase may give a conflict for every commit. Then a single merge is simpler.
Sign up to mark studied questions and save your progress.