Merging in git combines the work of two branches into a single branch. The usual case is merging a feature branch back into main after the feature is complete. Git automates this as much as possible, using a three-way merge algorithm that considers the common ancestor and the two branch tips.
git switch main
git merge feature # merge feature into main
git merge --no-ff feature # force a merge commit
git merge --squash feature # flatten into one commit
Three outcomes are possible. A fast-forward merge happens when main has not moved since the branch was created: git just moves the main pointer to the branch tip. A clean merge creates a new merge commit with two parents, representing the combined state. A conflict occurs when both branches changed the same lines; git pauses and asks you to resolve manually.
Conflict resolution:
git status # see which files conflict
$EDITOR conflicting-file # edit to resolve
git add conflicting-file
git commit # complete the merge
git merge --abort # give up and return
An alternative to merging is rebasing, which replays commits onto a new base without a merge commit. The choice between merging and rebasing is one of the enduring style debates in git workflows.
Related terms: Git, Git Branch, Git Rebase, conflict
Discussed in:
- Chapter 16: Version Control with Git — How Git Thinks
Also defined in: Textbook of Linux