git merge Command Guide
Join two or more development histories together
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
Basic
git merge <branch>Merge branch into currentgit merge --no-ff <branch>Always create merge commitgit merge --squash <branch>Squash into single commitConflicts
git merge --abortAbort mergegit merge --continueContinue after resolvinggit mergetoolOpen merge toolStrategy
-s recursiveDefault strategy-s oursKeep our changes-X theirsPrefer their changesDownloadable Image Preview
Basic Usage
The git merge command combines multiple branches together. You merge INTO your current branch FROM another branch.
# Merge feature into current branch
git checkout main
git merge feature-branch
# Merge with a merge commit (even if fast-forward possible)
git merge --no-ff feature-branch
# Preview merge without committing
git merge --no-commit feature-branchTypes of Merges
# Fast-forward merge (default when possible)
# Just moves branch pointer, no merge commit
git merge feature # If main has no new commits
# Three-way merge
# Creates a merge commit with two parents
git merge feature # When both branches have new commits
# No fast-forward (always create merge commit)
git merge --no-ff feature
# Squash merge (combine all commits)
git merge --squash feature
git commit -m "Merge feature (squashed)"Merge Types Comparison
- Fast-forward: Linear history, no merge commit
- Three-way: Preserves branch history with merge commit
- Squash: Single commit, loses individual history
Resolving Conflicts
# When merge has conflicts
git merge feature
# CONFLICT (content): Merge conflict in file.js
# Automatic merge failed
# View conflicted files
git status
# File content with conflict markers:
<<<<<<< HEAD
our version
=======
their version
>>>>>>> feature
# After editing to resolve:
git add file.js
git commit # Or: git merge --continue
# Abort merge entirely
git merge --abort
# Use merge tool
git mergetoolgit config --global merge.tool vscodeMerge Strategies
# Use recursive strategy (default)
git merge -s recursive feature
# Keep our version on conflicts
git merge -X ours feature
# Keep their version on conflicts
git merge -X theirs feature
# Completely ignore other branch (keep ours)
git merge -s ours featureMerge vs Rebase
# Merge - preserves history, creates merge commit
git checkout main
git merge feature
# History shows the branch and merge point
# Rebase - linear history, rewrites commits
git checkout feature
git rebase main
git checkout main
git merge feature # Fast-forward possible nowPractical Examples
# Standard feature merge
git checkout main
git pull
git merge --no-ff feature/login
git push
# Squash merge for clean history
git merge --squash feature/experiment
git commit -m "Add experimental feature"
# Merge specific commit
git cherry-pick abc1234
# Undo unpushed merge
git reset --hard HEAD~1
# Undo pushed merge
git revert -m 1 <merge-commit-sha>Frequently Asked Questions
What is a fast-forward merge?
When the target branch has no new commits since branching, Git simply moves the pointer forward. No merge commit is created. Use --no-ff to force a merge commit anyway.
How do I resolve merge conflicts?
Edit conflicted files (look for <<<<<<< markers), choose what to keep, remove the markers, then "git add" the files and "git commit" (or "git merge --continue").
What is the difference between merge and rebase?
Merge creates a merge commit preserving branch history. Rebase replays commits on top of another branch for a linear history. Use merge for shared branches, rebase for local/feature branches.
What does --squash do?
It combines all branch commits into staged changes without creating a merge commit. You then commit manually, resulting in a single commit with all the changes.
How do I undo a merge?
If not pushed: "git reset --hard HEAD~1". If pushed: "git revert -m 1 <merge-commit>" creates a new commit that undoes the merge.
Summary
git merge combines branches while preserving history. Use --no-ff for clear merge points in history.
Quick Reference
git merge <branch>- Merge branchgit merge --no-ff- Force merge commitgit merge --squash- Squash commitsgit merge --abort- Cancel merge
Official Documentation
For authoritative information, refer to the official documentation: