Synopsis
Description
Incorporates changes from the named commits (since the time their histories diverged from the current branch) into the current branch. This command is used bygit pull to incorporate changes from another repository and can be used by hand to merge changes from one branch into another.
For example, assume the following history exists with the current branch being master:
git merge topic will replay the changes made on the topic branch since it diverged from master (at E) and record the result in a new merge commit:
Common Usage
Merge a feature branch
First, switch to the branch you want to merge into:Then merge the feature branch:
Options
--no-commit
--no-commit
Perform the merge but don’t create a merge commit automatically. This allows you to inspect and further tweak the merge result before committing.
--squash
--squash
Produce the working tree and index state as if a real merge happened, but do not actually make a commit or move the HEAD. This allows you to create a single commit on top of the current branch.
--no-ff
--no-ff
Create a merge commit even when the merge resolves as a fast-forward. This is useful to maintain a clear history of feature branch merges.
--ff-only
--ff-only
Refuse to merge unless the current HEAD is already up to date or the merge can be resolved as a fast-forward.
-m <msg>
-m <msg>
Set the commit message for the merge commit.
-s <strategy>
-s <strategy>
Use the given merge strategy. Common strategies include:
ort(default) - Three-way merge algorithmrecursive- Three-way merge (legacy)resolve- Three-way merge using 3-way merge algorithmoctopus- For merging more than two branchesours- Merge but discard changes from other branchsubtree- Modified recursive strategy
-X <strategy-option>
-X <strategy-option>
Pass merge strategy-specific option. Common options:
ours- Auto-resolve conflicts by favoring our versiontheirs- Auto-resolve conflicts by favoring their versionignore-space-change- Ignore whitespace changes
--abort
--abort
Abort the current merge and try to reconstruct the pre-merge state.
--continue
--continue
After resolving conflicts, continue the merge process.
--quit
--quit
Forget about the current merge in progress, leaving the index and working tree as-is.
--allow-unrelated-histories
--allow-unrelated-histories
Examples
Basic merge
Using merge strategies
Merge without auto-commit
You should refrain from abusing the
--no-commit option to sneak substantial changes into a merge commit. Small fixups like bumping release/version name would be acceptable.Squash merge
Fast-Forward Merge
Often the current branch head is an ancestor of the named commit. This is the most common case when you haven’t made any local changes:How Conflicts Are Presented
When both sides change the same area of a file, Git cannot automatically resolve the conflict:How to Resolve Conflicts
Edit and resolve
Open conflicted files in your editor and resolve the conflicts by:
- Choosing one version
- Combining both versions
- Writing something completely different
<<<<<<<, =======, >>>>>>>)Tools for resolving conflicts
Pre-Merge Checks
git merge will refuse to proceed if:
- Local uncommitted changes overlap with files being updated
- The index differs from HEAD (unless specific merge strategies allow it)
Related Commands
- git branch - List, create, or delete branches
- git checkout - Switch branches
- git pull - Fetch from and integrate with another repository
- git mergetool - Run merge conflict resolution tools
- git reset - Reset current HEAD to specified state
