Git merge explained: merging branches
Merge is the operation that brings together work from multiple branches. Understand the different types of merges, conflicts, and how to resolve them with confidence.
What is git merge?
git merge combines the work of two branches into one. It's the fundamental operation of collaborative work with Git.
You create a branch
You work on your feature in a separate branch, without touching main.
You finish your work
Your changes are committed. Your branch is ready to be integrated.
You do the merge
You switch to main and merge your branch. The work is integrated.
The two types of merge
Fast-forward
When main has not received any new commits. Git simply moves the pointer forward. No merge commit, linear history.
Merge commit
When both branches have diverged. Git creates a special commit that combines changes from both sides.
Resolving a merge conflict
A conflict happens when Git cannot automatically decide which version to keep. It's not an error, it's a question.
1. Identify
git status shows you the conflicted files. Open them in your editor.
2. Resolve
Choose the version to keep, combine if necessary, and remove the conflict markers (<<<<<).
3. Confirm
git add then git commit to finalize the resolution.
Essential merge commands
git merge <branch>Merge the specified branch into the current branch
git merge --no-ff <branch>Force creating a merge commit (no fast-forward)
git merge --abortCancel a merge in progress (in case of conflict)
git mergetoolOpen a visual tool to resolve conflicts
git log --mergeShow the commits causing the conflict
git diffDuring a conflict, show the differences to resolve
Part of the Git Branching Workflow guide
GitQuest is created by Anaïs (nouvelle fenêtre), web developer and head of education, specializing in tech training and digital accessibility.
Questions about git merge
Ready to master merge?
Conflicts are no longer scary once you have practiced. Practice merge in a safe environment with GitQuest.
Practice merge