Ours vs theirs in conflicts in Git - Performance Comparison
When resolving conflicts in git, we often choose between "ours" and "theirs" changes. Understanding how the time to resolve conflicts grows helps us manage bigger projects smoothly.
We want to know how the effort scales as the number of conflicting files or changes increases.
Analyze the time complexity of this git conflict resolution snippet.
# Assume multiple conflicting files
for file in $(git diff --name-only --diff-filter=U); do
git checkout --ours "$file"
git add "$file"
done
This script loops over all files with conflicts, chooses "ours" version for each, and stages them.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over each conflicting file and running git commands on it.
- How many times: Once per conflicting file, so the number of conflicts determines the count.
As the number of conflicting files grows, the total commands run grow linearly.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 conflicts | About 20 git commands (checkout + add per file) |
| 100 conflicts | About 200 git commands |
| 1000 conflicts | About 2000 git commands |
Pattern observation: The work grows directly with the number of conflicts; doubling conflicts doubles the commands.
Time Complexity: O(n)
This means the time to resolve conflicts by choosing "ours" grows linearly with the number of conflicting files.
[X] Wrong: "Resolving conflicts with 'ours' or 'theirs' is instant no matter how many files conflict."
[OK] Correct: Each conflicting file requires a separate command to resolve and stage, so more conflicts mean more work and time.
Understanding how conflict resolution scales shows you grasp practical git workflows. This skill helps you manage merges smoothly in real projects, a valuable ability for any developer.
What if we changed the script to resolve conflicts in parallel instead of one by one? How would the time complexity change?