0
0
Gitdevops~5 mins

Merge conflicts why they happen in Git - Commands & Configuration

Choose your learning style9 modes available
Introduction
Merge conflicts happen when two people change the same part of a file in different ways. Git cannot decide which change to keep automatically, so it asks you to fix it.
When two developers edit the same line in a file and try to merge their changes.
When you update your branch but someone else changed the same code in the main branch.
When you rebase your work on top of new commits that touch the same files.
When merging feature branches that have overlapping changes.
When pulling changes from a remote repository that conflict with your local edits.
Commands
This command tries to merge the 'feature-branch' into your current branch. If both branches changed the same lines, Git will stop and show a conflict.
Terminal
git merge feature-branch
Expected OutputExpected
Auto-merging example.txt CONFLICT (content): Merge conflict in example.txt Automatic merge failed; fix conflicts and then commit the result.
Shows which files have conflicts and need your attention before you can finish the merge.
Terminal
git status
Expected OutputExpected
On branch main You have unmerged paths. (fix conflicts and run "git commit") Unmerged paths: (use "git add <file>..." to mark resolution) both modified: example.txt no changes added to commit (use "git add" and/or "git commit -a")
Shows the conflicting parts in the files so you can see what changed on each side.
Terminal
git diff
Expected OutputExpected
<<<<<<< HEAD Current branch changes ======= Feature branch changes >>>>>>> feature-branch
Marks the conflict in 'example.txt' as resolved after you fix it manually.
Terminal
git add example.txt
Expected OutputExpected
No output (command runs silently)
Completes the merge by committing the resolved changes.
Terminal
git commit -m "Resolve merge conflict in example.txt"
Expected OutputExpected
[main 1a2b3c4] Resolve merge conflict in example.txt
Key Concept

If you remember nothing else from this pattern, remember: merge conflicts happen when Git cannot automatically decide which change to keep because the same lines were changed differently.

Common Mistakes
Ignoring the conflict markers and committing the file as is.
This leaves conflict markers in the code, causing errors and broken code.
Manually edit the file to choose or combine changes, remove conflict markers, then add and commit.
Not running 'git add' after fixing conflicts before committing.
Git will not know the conflicts are resolved and will prevent the commit.
After fixing conflicts, run 'git add <file>' to mark them resolved before committing.
Trying to merge without pulling the latest changes first.
You may get unexpected conflicts or outdated code.
Always pull or fetch and rebase to update your branch before merging.
Summary
Run 'git merge' to combine branches; conflicts happen if the same lines differ.
Use 'git status' and 'git diff' to find and understand conflicts.
Manually fix conflicts, then 'git add' and 'git commit' to complete the merge.