Bird
Raised Fist0
Gitdevops~10 mins

Merge conflicts why they happen in Git - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Process Flow - Merge conflicts why they happen
Start with Branch A and Branch B
Both branches change same file
Git tries to merge changes
Check if changes overlap
Merge conflict
User resolves conflict
Merge completes
This flow shows how merge conflicts happen when two branches change the same part of a file and Git cannot automatically combine them.
Execution Sample
Git
git checkout branchA
# edit file.txt: change line 3
git commit -am "Change line 3 in branchA"

git checkout branchB
# edit file.txt: change line 3 differently
git commit -am "Change line 3 in branchB"

git checkout branchA
git merge branchB
This sequence shows two branches changing the same line in a file, then trying to merge, causing a conflict.
Process Table
StepBranchFile ChangeGit ActionResult
1branchAChange line 3 in file.txtCommitChange saved in branchA
2branchBChange line 3 differently in file.txtCommitChange saved in branchB
3branchANo changeCheckout branchASwitched to branchA
4branchA + branchBConflicting changes on line 3Merge branchB into branchAMerge conflict detected
5branchAUser edits file.txt to fix conflictResolve conflict manuallyConflict markers removed
6branchANo conflictCommit mergeMerge completed successfully
💡 Merge stops at conflict because Git cannot auto-merge overlapping changes.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 4After Step 5Final
file.txt line 3 contentOriginal line 3Changed by branchAChanged differently by branchBConflict markers addedConflict resolved by userMerged final content
Key Moments - 3 Insights
Why does Git stop the merge at step 4?
Git stops because both branches changed the same line differently, so it cannot decide which change to keep automatically (see execution_table step 4).
What does the user do to fix the conflict?
The user edits the file to choose or combine changes, removing conflict markers before committing (see execution_table step 5).
Why is it safe to commit after resolving conflicts?
Because the user manually fixed the overlapping changes, so the file is now consistent and ready to be merged (see execution_table step 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 4?
AUser commits changes
BGit automatically merges changes
CGit detects a merge conflict
DBranch is deleted
💡 Hint
Check the 'Result' column at step 4 in the execution_table
At which step does the user fix the conflict?
AStep 2
BStep 5
CStep 4
DStep 6
💡 Hint
Look for 'User edits file.txt to fix conflict' in the Git Action column
If branchB changed a different line than branchA, what would happen at step 4?
AMerge completes automatically
BUser must resolve conflict
CMerge conflict detected
DMerge is aborted
💡 Hint
Refer to the 'No' branch in the concept_flow after 'Check if changes overlap'
Concept Snapshot
Merge conflicts happen when two branches change the same part of a file.
Git tries to merge but stops if changes overlap.
User must manually fix conflicts by editing the file.
After resolving, commit to complete the merge.
Avoid conflicts by communicating and pulling often.
Full Transcript
Merge conflicts occur when two branches change the same line or part of a file differently. Git tries to merge changes automatically but stops if it cannot decide which change to keep. This is called a merge conflict. The user must open the file, see conflict markers, and edit the file to fix the conflict. After fixing, the user commits the changes to complete the merge. This process ensures the final file has consistent content from both branches.

Practice

(1/5)
1. Why do merge conflicts happen in Git?
easy
A. Because Git lost the commit history
B. Because the same part of a file was changed differently in two branches
C. Because the repository is too large
D. Because the branch names are the same

Solution

  1. Step 1: Understand what causes merge conflicts

    Merge conflicts happen when Git tries to combine changes but finds different edits in the same part of a file from two branches.
  2. Step 2: Identify the correct cause

    Only when the same lines are changed differently does Git stop and ask for help, causing a conflict.
  3. Final Answer:

    Because the same part of a file was changed differently in two branches -> Option B
  4. Quick Check:

    Same file part changed differently = merge conflict [OK]
Hint: Conflicts happen when edits overlap in the same file area [OK]
Common Mistakes:
  • Thinking conflicts happen due to repo size
  • Believing branch names cause conflicts
  • Assuming commit history loss causes conflicts
2. Which Git command is used to start a merge that might cause conflicts?
easy
A. git push
B. git branch
C. git clone
D. git merge

Solution

  1. Step 1: Identify the command that combines branches

    The git merge command is used to combine changes from one branch into another.
  2. Step 2: Understand when conflicts occur

    Conflicts can happen during this merge if changes overlap, so git merge is the command that triggers this process.
  3. Final Answer:

    git merge -> Option D
  4. Quick Check:

    git merge starts merges that may conflict [OK]
Hint: Use git merge to combine branches and check for conflicts [OK]
Common Mistakes:
  • Confusing git branch with git merge
  • Using git clone to merge
  • Thinking git push merges branches
3. Given two branches, master and feature, both changed the same line in app.txt. What will happen when you run git merge feature on master?
medium
A. Git will automatically merge without any issues
B. Git will delete app.txt
C. Git will create a merge conflict in app.txt
D. Git will ignore changes from feature branch

Solution

  1. Step 1: Analyze the changes in both branches

    Both branches changed the same line in app.txt, so Git cannot decide which change to keep automatically.
  2. Step 2: Understand Git's behavior on conflicting changes

    Git will stop the merge and mark app.txt as conflicted, requiring manual resolution.
  3. Final Answer:

    Git will create a merge conflict in app.txt -> Option C
  4. Quick Check:

    Same line changed differently = conflict created [OK]
Hint: Same line changed in both branches causes conflict [OK]
Common Mistakes:
  • Assuming Git merges automatically always
  • Thinking Git deletes files on conflict
  • Believing Git ignores conflicting changes
4. You ran git merge feature and got a conflict. Which step should you take to fix it?
medium
A. Edit the file to choose which changes to keep, then commit
B. Rename the branch and try merging again
C. Run git push immediately
D. Delete the conflicting file and commit

Solution

  1. Step 1: Understand how to resolve merge conflicts

    When a conflict occurs, you must open the conflicting file and decide which changes to keep or combine.
  2. Step 2: Complete the merge after resolving conflicts

    After editing and saving the file, you add it and commit to finish the merge process.
  3. Final Answer:

    Edit the file to choose which changes to keep, then commit -> Option A
  4. Quick Check:

    Resolve conflicts by editing files, then commit [OK]
Hint: Fix conflicts by editing files before committing [OK]
Common Mistakes:
  • Deleting files instead of resolving conflicts
  • Pushing before resolving conflicts
  • Renaming branches to fix conflicts
5. Two developers edited different parts of the same file in separate branches. When merging, why might Git NOT report a conflict?
hard
A. Because changes are in different lines and Git can merge automatically
B. Because Git ignores changes in one branch
C. Because the file was deleted in one branch
D. Because the branches have the same name

Solution

  1. Step 1: Understand Git's merge behavior with non-overlapping changes

    If changes are made in different parts of a file, Git can combine them automatically without conflicts.
  2. Step 2: Recognize why no conflict occurs

    Since the edits do not overlap, Git merges both changes smoothly.
  3. Final Answer:

    Because changes are in different lines and Git can merge automatically -> Option A
  4. Quick Check:

    Non-overlapping changes merge without conflict [OK]
Hint: Different lines changed merge automatically [OK]
Common Mistakes:
  • Thinking Git ignores one branch's changes
  • Assuming file deletion causes no conflict
  • Believing branch names affect conflicts