git merge

Branching & Merging

Join two or more development histories together


The 'git merge' command incorporates changes from the named commits (since the time their histories diverged from the current branch) into the current branch. Git performs a 'fast-forward' merge when possible (when the current branch is an ancestor of the merged branch), or creates a 'merge commit' when the histories have diverged. If there are conflicts between the branches, Git will pause and ask you to resolve them before completing the merge.

Synopsis

git merge [--no-ff | --ff-only] [--squash] [--no-commit] [-m <msg>] [--allow-unrelated-histories] [<commit>...]
git merge (--continue | --abort | --quit)
[ ] optional| or<arg> placeholder... repeatable--flag option

Options

Click any option to expand

Beginner (5)

Create a merge commit even when the merge resolves as a fast-forward. Preserves branch history and makes it clear where feature work started and ended.

When neededAlways records a merge commit — a team-convention choice

Example

git merge --no-ff feature

Refuse to merge unless the current HEAD is already up to date or the merge can be resolved as a fast-forward. Prevents accidental merge commits.

RecommendedNever creates surprise merge commits; fails if histories diverged

Example

git merge --ff-only feature

Abort the current conflict resolution process and restore the repository to the pre-merge state. Use when you want to cancel a conflicting merge.

RecommendedSafely backs out of a conflicted merge to the pre-merge state

Example

git merge --abort

After resolving merge conflicts, conclude the merge. Equivalent to running git commit after conflict resolution.

RecommendedFinishes the merge after resolving and staging the conflicts

Example

git merge --continue

Set a custom commit message for the merge commit. It does not force a merge commit: on a fast-forward the message is ignored (combine with --no-ff to always get one).

When neededCustom merge-commit message; ignored when the merge fast-forwards

Example

git merge -m "Integrate login feature" feature

Intermediate (2)

Combine all commits from the merged branch into a single set of staged changes. Does not create a commit or record a merge (no MERGE_HEAD) - you commit manually, and the result is a normal single-parent commit.

When neededOne clean commit, but not a real merge; the branch needs -D afterwards

Example

git merge --squash feature

Perform the merge but stop before creating a commit. Allows you to inspect and modify the merge result before committing. A fast-forward still happens; add --no-ff to guarantee a stop.

When neededInspect the result before committing; pair with --no-ff

Example

git merge --no-commit feature

Advanced (1)

Allow merging histories that do not share a common ancestor. Useful when combining two separate repositories or projects.

When neededOnly for intentionally joining separate projects

Example

git merge --allow-unrelated-histories other-project/main

Choosing the Right Option

Merge Strategies

How to control the type of merge Git performs

CommandCreates Merge CommitPreserves HistoryBest For
git merge (default)If divergedYesNormal feature integration
git merge --no-ffAlwaysYesDocumenting feature merges
git merge --ff-onlyNeverLinear onlyEnforcing clean history
git merge --squashNo (regular commit)No (flattened)Clean main branch

Handling Conflicts

What to do when a merge has conflicts

CommandActionKeeps WorkWhen to Use
(resolve manually)Edit files, git addYesNeed parts from both sides
--continueComplete mergeYesAfter resolving conflicts
--abortCancel mergePre-merge stateNot ready to resolve now
checkout --ours/--theirsPick one sideOne versionOne side is clearly correct

merge vs rebase

Two ways to integrate changes - different trade-offs

CommandHistoryConflict ResolutionBest For
git mergePreserves all commitsOnceShared branches, PRs
git rebaseRewrites to linearPer commitLocal cleanup, private branches
git merge --squashSingle commitOnceFeature with messy history

Controlling the Commit

Options for customizing the merge commit

CommandCommits AutomaticallyUse Case
git merge featureYes (if no conflicts)Standard merge
git merge --no-commitNo (staged)Review before commit
git merge -m "msg"Yes (ignored on fast-forward)Descriptive merge commits
git merge --squashNo (must commit)Combine all commits into one

Workflow Examples

1
git checkout main

Switch to the main branch

2
git pull origin main

Get the latest changes from remote

3
git merge --no-ff feature-branch

Merge feature with a merge commit for history

4
git push origin main

Push the merged changes to remote

5
git branch -d feature-branch

Delete the merged feature branch

1
git merge feature

Attempt the merge (conflicts occur)

2
git status

See which files have conflicts

3
(edit conflicted files)

Look for <<<<<<< markers and resolve manually

4
git add <resolved-files>

Stage the resolved files

5
git merge --continue

Complete the merge with a commit

1
git checkout main

Switch to the main branch

2
git merge --squash feature-branch

Combine all commits into staged changes

3
git commit -m "Add feature X"

Create one clean commit with all changes

4
git branch -D feature-branch

Force delete the feature branch

1
git merge feature

Start the merge (problems occur)

2
git status

See the conflicted state

3
git merge --abort

Cancel and restore pre-merge state

4
git status

Confirm you're back to clean state

Troubleshooting

CONFLICT (content): Merge conflict in file.js

Cause: Both branches modified the same lines in the file

Open the file, look for <<<<<<< markers, edit to resolve, then git add and git merge --continue

fatal: refusing to merge unrelated histories

Cause: The branches have no common ancestor (separate repositories)

Use git merge --allow-unrelated-histories if you intentionally want to combine separate projects

Already up to date

Cause: The branch you're merging is already included in your current branch

Nothing to do - your branch already contains all commits from the target

error: Your local changes to the following files would be overwritten by merge

Cause: You have uncommitted changes that conflict with the merge

Either commit your changes, stash them (git stash), or discard them before merging

Merge made but I wanted to review first

Cause: Git auto-committed the merge

Next time use --no-ff --no-commit. To redo: git reset --merge ORIG_HEAD, then merge again with --no-commit

Tips & Pitfalls

Best Practices

  • ✓Always pull the latest changes before merging to minimize conflicts
  • ✓Use --no-ff for feature branches to preserve the merge context in history
  • ✓Write descriptive merge commit messages that explain what was integrated
  • ✓Delete feature branches after merging to keep the repository clean
  • ✓Test your code after merging before pushing to ensure nothing broke
  • ✓Use git merge --abort if you're not ready to resolve conflicts - don't leave merges in a broken state
  • ✓For long-running branches, merge main into your feature branch periodically to reduce final merge conflicts

Common Mistakes

  • ✗Merging without pulling the latest changes from the remote first. Always 'git pull' before merging to avoid conflicts.
  • ✗Staging files that still contain conflict markers. Git blocks commits while paths are unmerged, but once you 'git add' a file it commits the <<<<<<< markers as-is.
  • ✗Using fast-forward merges for feature branches loses the branch history. Use '--no-ff' to preserve the context.
  • ✗Running 'git merge --abort' after manually resolving conflicts loses all conflict resolution work.
  • ✗Merging into the wrong branch. Always verify you're on the correct target branch with 'git branch' before merging.

FAQ

A:

Merge preserves history and creates a merge commit. Rebase rewrites history to be linear. Use merge for shared branches, rebase for local cleanup.

A:

Use --no-ff when merging feature branches to main. It creates a merge commit that documents when the feature was integrated, even if fast-forward was possible.

A:

If not pushed: git reset --hard ORIG_HEAD (works for fast-forwards too; discards uncommitted changes). If pushed: git revert -m 1 <merge-commit> to create a new commit that undoes the merge.

A:

Fast-forward happens when your branch has no new commits since the branch point. Git just moves the pointer forward instead of creating a merge commit.

A:

Open conflicted files, look for <<<<<<< markers, edit to keep the code you want, remove the markers, then git add the files and git merge --continue.

A:

Regular merge preserves all commit history. Squash merge combines everything into one commit. Use squash when the feature branch has messy WIP commits you don't want in main.