Skip to content
$ lynuxpro
← Git

LESSON 8 OF 21 · INTERMEDIATE · 20 MIN

Merging and merge conflicts

HOW IT WORKSFast-forward, merge commit and conflict

c2common ancestorc3 (main)Describe shopc4 (feature)Add cartMerge committwo parentsConflictsame lines changedResolvededit, add, commit
1/5

If main has no new commits, merging just moves the main pointer forward: a fast-forward.

$ git merge feature/cart
Fast-forward

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"
SHELLbashutf-8 · 7L

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/readme
SHELLbashutf-8 · 6L
Updating 1a2b3c4..5d6e7f8Fast-forward README.md |   1 + 1 file changed, 1 insertion(+)
OUTPUTterminalutf-8 · 4L

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 --graph
SHELLbashutf-8 · 8L
Auto-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 greetings
OUTPUTterminalutf-8 · 11L

Both branches changed app.py, but different lines, so Git merged them automatically. "ort" is Git's default merge strategy.

cat app.py
SHELLbashutf-8 · 1L
def greet():    return "Hello there"def farewell():    return "Goodbye, see you soon"
OUTPUTterminalutf-8 · 6L

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/hindi
SHELLbashutf-8 · 7L
Auto-merging app.pyCONFLICT (content): Merge conflict in app.pyAutomatic merge failed; fix conflicts and then commit the result.
OUTPUTterminalutf-8 · 3L

Step 1: see what is conflicted

git status
SHELLbashutf-8 · 1L
On 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.py
OUTPUTterminalutf-8 · 8L

Step 2: read the conflict markers

cat app.py
SHELLbashutf-8 · 1L
def greet():<<<<<<< HEAD    return "Hola"=======    return "Namaste">>>>>>> feature/hindidef farewell():    return "Goodbye, see you soon"
OUTPUTterminalutf-8 · 10L
  • Between <<<<<<< HEAD and =======: 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.py
SHELLbashutf-8 · 2L
0
OUTPUTterminalutf-8 · 1L

Always 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 4
SHELLbashutf-8 · 4L
On 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/farewell
OUTPUTterminalutf-8 · 14L

git 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 --abort
SHELLbashutf-8 · 1L

Seeing 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)
SHELLbashutf-8 · 2L

Avoiding painful conflicts

Conflicts grow with the time branches live apart and the amount of code they touch:

  • Merge or rebase from main often (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 X brings X into the branch you are on. Check git status first.
  • Panicking mid-merge. git merge --abort puts everything back.
  • Not testing after a merge. Both sides can be correct alone and wrong together.

Practice

  1. Create a branch, add a file, and merge it into main with a fast-forward.
  2. 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.
  3. Create a conflict on purpose, look at the markers with cat, resolve it, and complete the merge.
  4. 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 true
OUTPUTterminalutf-8 · 10L

Key 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, then git commit.
  • git merge --abort cancels a merge safely.
  • Merge from main often and keep branches small to keep conflicts rare and tiny.