Resolving merge conflicts in Git - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When merging branches in git, conflicts can happen if changes overlap. Understanding how the time to resolve conflicts grows helps us plan better.
We want to know: how does the effort to fix conflicts change as the conflicting changes grow?
Analyze the time complexity of resolving conflicts after a merge attempt.
git checkout feature-branch
git merge main
# If conflicts appear, open files and edit conflict markers
# After fixing, run:
git add <fixed-files>
git commit -m "Resolve merge conflicts"
This snippet shows the steps to merge and resolve conflicts manually.
Look for repeated work when resolving conflicts.
- Primary operation: Manually reviewing and editing each conflicting file.
- How many times: Once per conflicting file, and within each file, per conflicting section.
The time to resolve conflicts grows with the number of conflicting files and conflict sections inside them.
| Input Size (conflicts) | Approx. Operations (edits) |
|---|---|
| 10 conflict sections | 10 edits |
| 100 conflict sections | 100 edits |
| 1000 conflict sections | 1000 edits |
Pattern observation: The effort 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 sections you must fix.
[X] Wrong: "Resolving one conflict fixes all conflicts automatically."
[OK] Correct: Each conflict is separate and must be fixed manually; fixing one does not solve others.
Understanding how conflict resolution scales shows you can manage code changes smoothly and keep projects moving forward.
"What if we used a merge tool that automatically resolves some conflicts? How would the time complexity change?"
Practice
Solution
Step 1: Understand merge conflict meaning
A merge conflict happens when Git sees changes in the same part of a file from different branches and cannot decide which to keep.Step 2: Identify what Git does in this case
Git stops the merge and marks the conflict in the file for you to fix manually.Final Answer:
Git found changes in the same file that it cannot combine automatically. -> Option AQuick Check:
Merge conflict = conflicting changes in same file [OK]
- Thinking Git merges all changes automatically
- Confusing conflict with branch creation
- Assuming files are deleted automatically
Solution
Step 1: Identify how to tell Git conflict is fixed
After editing the conflicted file, you must stage it to tell Git the conflict is resolved.Step 2: Choose the correct command to stage files
The command to stage files isgit add <file>.Final Answer:
git add <file> -> Option AQuick Check:
Stage resolved file with git add [OK]
- Trying to commit before staging resolved files
- Using git merge --continue without staging
- Using invalid commands like git checkout --conflict
<<<<<<< HEAD
Line A
=======
Line B
>>>>>>> feature-branchWhat will the file content be after you keep only the changes from the feature-branch and stage the file?Solution
Step 1: Understand conflict markers
The lines between <<<<<<< HEAD and ======= are from current branch; lines between ======= and >>>>>>> feature-branch are from the other branch.Step 2: Keep only feature-branch changes
To keep only feature-branch changes, remove the markers and the HEAD section, leaving just 'Line B'.Final Answer:
Line B -> Option BQuick Check:
Keep feature-branch changes = Line B [OK]
- Leaving conflict markers in file
- Keeping both changes without cleaning markers
- Confusing which side is which branch
git commit now?Solution
Step 1: Understand commit behavior during merge conflicts
Git requires you to stage resolved files before committing a merge. If files are not staged, Git sees conflicts as unresolved.Step 2: What happens when committing without staging
Git will refuse to commit and show an error about unmerged paths, preventing incomplete merges.Final Answer:
Git refuses to commit and shows an error about unmerged paths. -> Option CQuick Check:
Commit without staging resolved files = error [OK]
- Assuming git commit auto-stages files
- Thinking Git commits partial merges
- Expecting merge to abort automatically
Solution
Step 1: Understand the goal to keep all changes
Since you want to keep both branches' changes, you must manually combine them in the file.Step 2: How to resolve conflicts properly
Edit the file to merge both changes, remove conflict markers, then stage withgit addand commit the merge.Final Answer:
Manually edit the file to combine both changes, remove conflict markers, then stage and commit. -> Option DQuick Check:
Manual merge + stage + commit = keep all changes [OK]
- Aborting merge instead of resolving
- Deleting files losing changes
- Resetting hard losing all work
