Overview
The way you integrate a branch decides the shape of history: a merge commit, a straight line, or one squashed commit. Choose before merging, because changing it later rewrites shared history. For daily commands see git-commands; for rebase recipes see git-rebase.
Integration styles
Choose the style from your branch policy: linear history, merge commits, or one commit per PR.
| Command | Resulting history | Use when |
|---|---|---|
git merge feature | Fast-forward if possible, else a merge commit. | The default; fine for most flows. |
git merge --ff-only feature | Linear; fails if a fast-forward is impossible. | Enforcing “rebase before merge”. |
git merge --no-ff feature | Always a merge commit. | The branch boundary matters for audit or revert. |
git merge --squash feature, then git commit | One commit; no parent link to the branch. | One reviewable commit per PR (GitHub “Squash and merge”). |
git rebase main, then git merge --ff-only | Linear; the branch’s SHAs change. | Clean history for local, not-yet-pushed work. |
Ours and theirs
The sides swap between commands.
| Context | ours | theirs |
|---|---|---|
git merge feature | Current branch (HEAD). | feature. |
git rebase main | main, the new base. | Your commit being replayed. |
git cherry-pick <ref> | Current branch (HEAD). | The picked commit. |
| Command | Effect |
|---|---|
git checkout --ours <file> / --theirs <file> | Take one side of a conflicted file. |
git merge -X ours / -X theirs | Prefer one side only for conflicting hunks; other changes still merge. |
git merge -s ours | Record a merge but keep the current tree; the other branch’s changes are discarded. |
Strategies (-s)
Leave the strategy at its default unless you need ours, subtree, or octopus.
| Strategy | Notes |
|---|---|
ort | Default for a two-head merge; handles renames. recursive is now a synonym. |
resolve | Two-head three-way merge; does not handle renames. |
octopus | Default for three or more heads; refuses merges that need manual resolution. |
ours | Keeps the current tree whatever the other heads contain. |
subtree | ort adjusted for merging a project into a subdirectory. |
Gotchas
Avoid these; each leaves a branch undeleted, drops changes, or creates stray merge commits.
| Gotcha | Fix |
|---|---|
git branch -d fails after a squash merge; git cannot see it as merged. | Confirm the squash landed, then git branch -D. |
-s ours and -X ours look alike but differ. | -s drops the other side entirely; -X only resolves conflicts. |
A merge without --ff-only can create merge commits you did not intend. | git config --global merge.ff only or pull.ff only. |
| Rebasing pushed commits breaks teammates’ clones. | Rebase before the first push; afterwards merge. |
-X theirs hides real conflicts. | Resolve by hand when the conflict is semantic. |