Merge
In short
A merge in Git combines the changes from one branch into another, joining separate lines of development back together into a single, shared history.
What is a merge in Git?
Merging is how work done on separate branches comes back together. When you merge a feature branch into main, Git integrates the changes from the feature branch's commits, so main now includes the new work. The history of both branches is preserved.
Git merges in one of two main ways. If the target branch hasn't changed since the feature branch was created, Git performs a fast-forward merge, which simply moves the branch pointer ahead. If both branches have new commits, Git performs a three-way merge: it compares each branch with their common ancestor and records the combined result in a new merge commit that has two parents.
A merge conflict happens when both branches changed the same lines of the same file, so Git can't decide which version to keep. Git marks the conflicting sections with <<<<<<<, =======, and >>>>>>> markers, and you must edit the file, keep the correct content, and commit the result. It is like two people editing the same sentence in a shared document: someone has to decide the final wording.
Merge is often compared with rebase. Both bring changes from one branch into another, but merge keeps the history exactly as it happened, including merge commits, while rebase rewrites commits to create a straight, linear history. Merging is generally the safer choice for branches that other people also use.
At a glance
Key takeaways
- A merge integrates changes from one branch into another.
- A fast-forward merge just moves the pointer; a three-way merge creates a merge commit.
- Conflicts occur when both branches change the same lines and must be resolved by hand.
- Merge preserves history, while rebase rewrites it into a straight line.
Example
# Switch to the branch you want to merge into
git switch main
# Bring in the changes from the feature branch
git merge feature/search-bar
# If there is a conflict: fix the marked files, then
git add src/search.ts
git commit # completes the merge
# Or cancel the merge and go back to how things were
git merge --abortReaders ask
What is a merge conflict?
A merge conflict occurs when two branches change the same part of a file in different ways and Git can't combine them automatically. You resolve it by editing the file to the correct final version, staging it with git add, and committing.
What is the difference between merge and rebase?
Merge combines branches and keeps their full history, adding a merge commit when needed. Rebase moves your commits on top of another branch, rewriting them to produce a clean, linear history.
What is a squash merge?
A squash merge combines all the commits from a branch into a single new commit on the target branch. It keeps the main history tidy, at the cost of losing the individual commits from the feature branch.
Often compared
See also
- GitVersion Control, p. 10Git is a free, open-source distributed version control system that tracks changes to files over time, so developers can collaborate and undo mistakes.
- BranchVersion Control, p. 2A branch in Git is an independent line of development that lets you work on a feature or fix in isolation, without affecting the main code until you merge it.
- RebaseVersion Control, p. 33A rebase in Git replays a branch's commits on top of another commit, rewriting history to produce a cleaner, linear sequence of changes without merge commits.
- Merge ConflictVersion Control, p. 30A merge conflict happens when Git can't automatically combine two branches because both changed the same lines of a file, so a person must decide the result.
- CommitVersion Control, p. 5A commit is a saved snapshot of a project's files in Git, recorded with a unique ID, an author, a timestamp, and a message describing what changed.
- Pull RequestVersion Control, p. 32A pull request is a proposal to merge changes from one branch into another, giving teammates a place to review, discuss, and test the code before it is merged.
- Fast-Forward MergeVersion Control, p. 8A fast-forward merge happens when the target has no new commits since the other branch split off, so Git just moves its pointer ahead with no merge commit.
Sources
Spotted a mistake or something missing on this page?Suggest an edit