Merge conflicts why they happen in Git - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to resolve merge conflicts grows as the changes increase.
How does the number of conflicting changes affect the work needed to fix them?
Analyze the time complexity of resolving merge conflicts during a git merge.
git checkout feature-branch
# Make changes in feature-branch
git checkout main
# Make changes in main branch
git merge feature-branch
# Conflict occurs if same lines changed
# User manually edits conflict markers
# User stages resolved files
This snippet shows switching branches, making changes, merging, and resolving conflicts manually.
Look for repeated work when resolving conflicts.
- Primary operation: Checking and editing each conflicting file and each conflicting section inside it.
- How many times: Once per conflicting section, which depends on how many overlapping changes exist.
More conflicting changes mean more sections to fix.
| Input Size (conflicting sections) | Approx. Operations (edits) |
|---|---|
| 10 | About 10 manual edits |
| 100 | About 100 manual edits |
| 1000 | About 1000 manual edits |
Pattern observation: The work grows roughly in direct proportion to the number of conflicts.
Time Complexity: O(n)
This means the time to resolve conflicts grows linearly with the number of conflicting changes.
[X] Wrong: "Resolving one conflict fixes all conflicts automatically."
[OK] Correct: Each conflict is separate and must be fixed individually, so time grows with conflicts.
Understanding how conflict resolution time grows helps you plan merges and communicate with your team calmly.
"What if we used a tool that automatically merges some conflicts? How would that affect the time complexity?"
Practice
Solution
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.Step 2: Identify the correct cause
Only when the same lines are changed differently does Git stop and ask for help, causing a conflict.Final Answer:
Because the same part of a file was changed differently in two branches -> Option BQuick Check:
Same file part changed differently = merge conflict [OK]
- Thinking conflicts happen due to repo size
- Believing branch names cause conflicts
- Assuming commit history loss causes conflicts
Solution
Step 1: Identify the command that combines branches
Thegit mergecommand is used to combine changes from one branch into another.Step 2: Understand when conflicts occur
Conflicts can happen during this merge if changes overlap, sogit mergeis the command that triggers this process.Final Answer:
git merge -> Option DQuick Check:
git mergestarts merges that may conflict [OK]
- Confusing git branch with git merge
- Using git clone to merge
- Thinking git push merges branches
app.txt. What will happen when you run git merge feature on master?Solution
Step 1: Analyze the changes in both branches
Both branches changed the same line inapp.txt, so Git cannot decide which change to keep automatically.Step 2: Understand Git's behavior on conflicting changes
Git will stop the merge and markapp.txtas conflicted, requiring manual resolution.Final Answer:
Git will create a merge conflict in app.txt -> Option CQuick Check:
Same line changed differently = conflict created [OK]
- Assuming Git merges automatically always
- Thinking Git deletes files on conflict
- Believing Git ignores conflicting changes
git merge feature and got a conflict. Which step should you take to fix it?Solution
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.Step 2: Complete the merge after resolving conflicts
After editing and saving the file, you add it and commit to finish the merge process.Final Answer:
Edit the file to choose which changes to keep, then commit -> Option AQuick Check:
Resolve conflicts by editing files, then commit [OK]
- Deleting files instead of resolving conflicts
- Pushing before resolving conflicts
- Renaming branches to fix conflicts
Solution
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.Step 2: Recognize why no conflict occurs
Since the edits do not overlap, Git merges both changes smoothly.Final Answer:
Because changes are in different lines and Git can merge automatically -> Option AQuick Check:
Non-overlapping changes merge without conflict [OK]
- Thinking Git ignores one branch's changes
- Assuming file deletion causes no conflict
- Believing branch names affect conflicts
