branching
git merge
Joins the history of another branch into the current one, creating a merge commit when histories diverge. Preserves the full branching topology.
git merge [<options>] [<commit>...] Common flags
| Flag | Purpose |
|---|---|
| --no-ff | Always create a merge commit even if fast-forward is possible |
| --ff-only | Refuse the merge unless it's a clean fast-forward |
| --squash | Combine changes into a single commit on the current branch |
| --abort | Cancel an in-progress conflicted merge |
| --continue | Resume after resolving conflicts |
| -s <strategy> | Choose merge strategy (ort, recursive, ours, etc.) |
Examples
git merge feature/login Merge the feature branch into the current one
git merge --no-ff release/1.2 Preserve branch topology with an explicit merge commit
git merge --abort Bail out of a conflicted merge and restore pre-merge state
Gotcha
After resolving conflicts, you must 'git add' the fixed files and 'git commit' (or 'git merge --continue') — running the wrong command can leave the merge half-finished.
Related commands
git rebase
Replays commits from your branch on top of another base, producing a linear history without merge commits. Powerful for cleaning up history before pushing.
git branch
Lists, creates, renames, or deletes branches — pointers to commits. Does not switch branches on its own; use switch or checkout for that.
git pull
Fetches from the remote and integrates the changes into the current branch, effectively 'git fetch' followed by 'git merge' (or rebase). Updates your working tree with upstream commits.
git checkout
Legacy multipurpose command that switches branches, restores files, or detaches HEAD to a commit. In modern Git (2.23+), prefer 'git switch' for branches and 'git restore' for files.
git switch
Modern, focused command (Git 2.23+) that only switches branches — no file restoration overloading like checkout. Safer and clearer for everyday branch changes.