Skip to main content

Merge

Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/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

Two ways Git merges: when both branches have new commits, a three-way merge creates merge commit M with two parents, C from main and E from feature; when main has not moved, a fast-forward merge just moves the main pointer ahead to E.Three-way mergeboth movedparent 1parent 2ABCMDEcommon ancestormerge commitmainfeatureFast-forwardmain didn't moveABDEmainmoves aheadmainfeature
When both branches moved, Git joins them with a new commit that has two parents. When main didn't move, it just slides main forward.

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

Merging a branch and handling a conflictbash
# 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 --abort

Readers 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

Sources

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings