git merge
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)Options
Choosing the Right Option
Merge Strategies
How to control the type of merge Git performs
| Command | Creates Merge Commit | Preserves History | Best For |
|---|---|---|---|
git merge (default) | If diverged | Yes | Normal feature integration |
git merge --no-ff | Always | Yes | Documenting feature merges |
git merge --ff-only | Never | Linear only | Enforcing clean history |
git merge --squash | No (regular commit) | No (flattened) | Clean main branch |
Handling Conflicts
What to do when a merge has conflicts
| Command | Action | Keeps Work | When to Use |
|---|---|---|---|
(resolve manually) | Edit files, git add | Yes | Need parts from both sides |
--continue | Complete merge | Yes | After resolving conflicts |
--abort | Cancel merge | Pre-merge state | Not ready to resolve now |
checkout --ours/--theirs | Pick one side | One version | One side is clearly correct |
merge vs rebase
Two ways to integrate changes - different trade-offs
| Command | History | Conflict Resolution | Best For |
|---|---|---|---|
git merge | Preserves all commits | Once | Shared branches, PRs |
git rebase | Rewrites to linear | Per commit | Local cleanup, private branches |
git merge --squash | Single commit | Once | Feature with messy history |
Controlling the Commit
Options for customizing the merge commit
| Command | Commits Automatically | Use Case |
|---|---|---|
git merge feature | Yes (if no conflicts) | Standard merge |
git merge --no-commit | No (staged) | Review before commit |
git merge -m "msg" | Yes (ignored on fast-forward) | Descriptive merge commits |
git merge --squash | No (must commit) | Combine all commits into one |
Workflow Examples
git checkout mainSwitch to the main branch
git pull origin mainGet the latest changes from remote
git merge --no-ff feature-branchMerge feature with a merge commit for history
git push origin mainPush the merged changes to remote
git branch -d feature-branchDelete the merged feature branch
git merge featureAttempt the merge (conflicts occur)
git statusSee which files have conflicts
(edit conflicted files)Look for <<<<<<< markers and resolve manually
git add <resolved-files>Stage the resolved files
git merge --continueComplete the merge with a commit
git checkout mainSwitch to the main branch
git merge --squash feature-branchCombine all commits into staged changes
git commit -m "Add feature X"Create one clean commit with all changes
git branch -D feature-branchForce delete the feature branch
git merge featureStart the merge (problems occur)
git statusSee the conflicted state
git merge --abortCancel and restore pre-merge state
git statusConfirm 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
Merge preserves history and creates a merge commit. Rebase rewrites history to be linear. Use merge for shared branches, rebase for local cleanup.
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.
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.
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.
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.
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.