Branches are only useful if their work can be combined again. git merge brings the commits from one branch into another. Most merges happen automatically; sometimes two branches change the same lines and Git asks you to decide, which is called a merge conflict. Conflicts are normal and not scary once you know how to read them. This lesson covers both kinds of merge, resolving conflicts step by step, and how to avoid most conflicts in the first place.
In simple words
- Merge: bringing the changes from one branch into another.
- Fast-forward merge: when the target branch has no new commits, Git just moves it forward. No merge commit is needed.
- Merge commit: a commit with two parents that joins two branches that both moved on.
- Merge conflict: when both branches changed the same lines, so Git asks you to choose the final version.
Set up two branches
git config --global user.name "Leela Kumar"mkdir merge-labcd merge-labgit initprintf 'def greet():\n return "Hello"\n\n\ndef farewell():\n return "Bye"\n' > app.pygit add app.pygit commit -m "Add greetings"1. Fast-forward merge
When the branch you merge into has no new commits since the other branch started, Git can simply move the pointer forward. No new commit is needed:
git switch -c feature/readmeprintf '# Greeter\n' > README.mdgit add README.mdgit commit -m "Add README"git switch maingit merge feature/readmeUpdating 1a2b3c4..5d6e7f8Fast-forward README.md | 1 + 1 file changed, 1 insertion(+)main now points at the same commit as feature/readme. History stays a straight line.
2. Three-way merge (merge commit)
When both branches have new commits, Git finds their merge base (the last commit they share), compares each branch with it, and combines the changes into a new merge commit with two parents:
git switch -c feature/farewellprintf 'def greet():\n return "Hello"\n\n\ndef farewell():\n return "Goodbye, see you soon"\n' > app.pygit commit -am "Friendlier farewell"git switch mainprintf 'def greet():\n return "Hello there"\n\n\ndef farewell():\n return "Bye"\n' > app.pygit commit -am "Warmer greeting"git merge feature/farewell -m "Merge feature/farewell"git log --oneline --graphAuto-merging app.pyMerge made by the 'ort' strategy. app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)* 9e8d7c6 (HEAD -> main) Merge feature/farewell|\* | 3b2a1f0 Warmer greeting| * 4c5b6a7 (feature/farewell) Friendlier farewell|/* 5d6e7f8 (feature/readme) Add README* 1a2b3c4 Add greetingsBoth branches changed app.py, but different lines, so Git merged them automatically. "ort" is Git's default merge strategy.
cat app.pydef greet(): return "Hello there"def farewell(): return "Goodbye, see you soon"Forcing a merge commit
Some teams want a merge commit even when a fast-forward is possible, so the history shows that a feature was developed on a branch. Use git merge --no-ff branch. GitLab's merge request settings offer the same choice ("Merge commit", "Merge commit with semi-linear history", "Fast-forward merge").
3. Merge conflicts
A conflict happens when both branches change the same lines (or one deletes a file the other changed). Git cannot know which version is right, so it stops and asks you.
git switch -c feature/hindiprintf 'def greet():\n return "Namaste"\n\n\ndef farewell():\n return "Goodbye, see you soon"\n' > app.pygit commit -am "Greet in Hindi"git switch mainprintf 'def greet():\n return "Hola"\n\n\ndef farewell():\n return "Goodbye, see you soon"\n' > app.pygit commit -am "Greet in Spanish"git merge feature/hindiAuto-merging app.pyCONFLICT (content): Merge conflict in app.pyAutomatic merge failed; fix conflicts and then commit the result.Step 1: see what is conflicted
git statusOn branch mainYou have unmerged paths. (fix conflicts and run "git commit") (use "git merge --abort" to abort the merge)Unmerged paths: (use "git add <file>..." to mark resolution) both modified: app.pyStep 2: read the conflict markers
cat app.pydef greet():<<<<<<< HEAD return "Hola"======= return "Namaste">>>>>>> feature/hindidef farewell(): return "Goodbye, see you soon"- Between
<<<<<<< HEADand=======: your version (the branch you are on,main). - Between
=======and>>>>>>> feature/hindi: their version (the branch being merged). - Everything outside the markers merged cleanly.
Step 3: edit the file to the correct result
You decide. Keep one side, the other, or write something combining both. Then remove all three marker lines. Here, the team decides to support both languages:
printf 'def greet(lang="hi"):\n return "Namaste" if lang == "hi" else "Hola"\n\n\ndef farewell():\n return "Goodbye, see you soon"\n' > app.pygrep -c '<<<<<<<' app.py0Always check that no markers are left: grep -rn '<<<<<<<' . across the project is a good habit. In an editor such as VS Code, conflicts are highlighted with buttons for "Accept Current", "Accept Incoming" and "Accept Both".
Step 4: mark as resolved and commit
git add app.pygit statusgit commit -m "Merge feature/hindi: support both languages"git log --oneline --graph -n 4On branch mainAll conflicts fixed but you are still merging. (use "git commit" to conclude merge)Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: app.py[main 7f6e5d4] Merge feature/hindi: support both languages* 7f6e5d4 (HEAD -> main) Merge feature/hindi: support both languages|\* | 8a9b0c1 Greet in Spanish| * 2c3d4e5 (feature/hindi) Greet in Hindi|/* 9e8d7c6 Merge feature/farewellgit add on a conflicted file means "resolved". After the commit, run your tests: a clean merge is not the same as working code.
Changing your mind: git merge --abort
If a merge gets confusing, go back to exactly how things were before you started:
git merge --abortSeeing what a merge will do, before merging
git log --oneline main..feature/x # commits that would come ingit diff main...feature/x # their changes since the branches split (three dots)Avoiding painful conflicts
Conflicts grow with the time branches live apart and the amount of code they touch:
- Merge or rebase from
mainoften (daily), so you resolve small conflicts early instead of one huge one at the end. - Keep branches small and short-lived. A one-day branch rarely conflicts.
- Do not mix reformatting with real changes. A formatter run across a whole file conflicts with everyone. Do it in a separate, agreed commit.
- Communicate when you are about to restructure a shared file.
- Turn on
rerere(git config --global rerere.enabled true) so Git remembers how you resolved a conflict and reapplies it if the same one appears again.
Real-world scenario: the config file everyone edits
A platform team kept all service settings in one values.yaml file. Every merge request touched it, and conflicts appeared several times a day, usually at the very end of a long-running branch. They made two changes: they split the file into one file per service, and they adopted a rule of merging main into feature branches every morning. Conflicts dropped to a few a week, and the ones that remained were small enough to resolve in a minute.
Common mistakes
- Leaving conflict markers in a file and committing it. Search for
<<<<<<<before committing; many CI pipelines and pre-commit hooks check for this. - Picking one side blindly ("accept all incoming") without understanding the other change.
- Merging the wrong direction.
git merge Xbrings X into the branch you are on. Checkgit statusfirst. - Panicking mid-merge.
git merge --abortputs everything back. - Not testing after a merge. Both sides can be correct alone and wrong together.
Practice
- Create a branch, add a file, and merge it into
mainwith a fast-forward. - Create two branches that change different lines of the same file, and merge them with a merge commit. View the result with
git log --oneline --graph. - Create a conflict on purpose, look at the markers with
cat, resolve it, and complete the merge. - Start another conflicting merge and cancel it with
git merge --abort.
Cheat sheet
git merge branch merge branch into the current onegit merge --no-ff branch always create a merge commitgit merge --abort cancel a merge in progressgit status list conflicted files ("both modified")<<<<<<< HEAD … ======= … >>>>>>> branch conflict markersgit add file mark a conflict as resolvedgit commit finish the mergegit log --oneline main..branch commits a merge would bring ingit diff main...branch changes since the branches splitgit config --global rerere.enabled trueKey takeaways
- A fast-forward just moves the branch pointer; a three-way merge creates a merge commit with two parents.
- Conflicts happen when both branches change the same lines; Git marks them with
<<<<<<<,=======and>>>>>>>. - Resolve by editing to the correct result, removing all markers,
git add, thengit commit. git merge --abortcancels a merge safely.- Merge from
mainoften and keep branches small to keep conflicts rare and tiny.