Git Reference / Merging & rebasing
07Merging & rebasing
Two ways to integrate work. Merge preserves history exactly as it happened. Rebase replays your commits on top of another branch for a linear history.
Merge
Combine another branch into the current one.
git switch main git merge feature git merge --no-ff feature # always create merge commit git merge --squash feature # one combined change git merge --abort
Rebase
Move your branch to start from a newer base.
git switch feature
git rebase main
git rebase --continue # after fixing conflicts
git rebase --abort
git rebase --onto main old-base featureResolve conflicts
Edit the <<<<<<< markers, then stage.
git status # see conflicted files git checkout --ours file.txt # keep our version git checkout --theirs file.txt # keep their version git add file.txt git mergetool
Better conflict markers
Show the common ancestor too — far easier to reason about.
git config --global merge.conflictstyle zdiff3
git config --global rerere.enabled true # remember resolutionsThe golden rule of rebasingNever rebase commits that others have already pulled. Rebasing rewrites hashes; shared history should only move forward.