Ours vs theirs in conflicts in Git - Performance Comparison
Start learning this pattern below
Jump into concepts and practice - no test required
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?
Practice
ours refer to?Solution
Step 1: Understand the meaning of 'ours' in Git conflicts
'Ours' means the version of the file in your current branch where you started the merge.Step 2: Differentiate from 'theirs'
'Theirs' refers to the version from the branch you are merging into your current branch.Final Answer:
The version of the file in your current branch -> Option DQuick Check:
Ours = current branch version [OK]
- Confusing 'ours' with 'theirs'
- Thinking 'ours' means the common ancestor
- Assuming 'ours' is a backup copy
app.js?Solution
Step 1: Identify the correct command to pick 'theirs'
The command to choose the 'theirs' version during conflict isgit checkout --theirs <file>.Step 2: Verify the file name usage
Usingapp.jsafter the command specifies the file to resolve.Final Answer:
git checkout --theirs app.js -> Option BQuick Check:
Use 'git checkout --theirs' to pick theirs [OK]
- Using 'git reset' instead of 'git checkout'
- Confusing '--ours' with '--theirs'
- Trying 'git merge --theirs' which is invalid
index.html, you run:git checkout --ours index.htmlWhat will be the content of
index.html after this command?Solution
Step 1: Understand what 'git checkout --ours' does in conflict
This command replaces the conflicted file with the version from your current branch before the merge.Step 2: Confirm the file content after the command
After running it,index.htmlwill have your branch's original content, ignoring the other branch's changes.Final Answer:
The version from your current branch before the merge -> Option AQuick Check:
'git checkout --ours' = current branch content [OK]
- Thinking it picks the other branch's version
- Assuming it resets to common ancestor
- Expecting it to merge both versions automatically
git checkout --theirs main.js but the file did not update. What is the likely cause?Solution
Step 1: Check the merge state requirement
Thegit checkout --theirscommand only works when Git detects a conflict during a merge.Step 2: Understand why the file didn't update
If you are not in a conflicted state, Git has no 'theirs' version to apply, so the file stays unchanged.Final Answer:
You are not in a conflicted merge state -> Option CQuick Check:
'git checkout --theirs' needs conflict state [OK]
- Trying to use --theirs outside a merge conflict
- Assuming checkout updates file without conflict
- Not verifying current branch or file name
config.yaml but accept the other branch's version for README.md. Which sequence of commands correctly resolves this?Solution
Step 1: Use correct commands to pick versions per file
To keep your branch's version forconfig.yaml, usegit checkout --ours config.yaml. To accept the other branch's version forREADME.md, usegit checkout --theirs README.md.Step 2: Stage the resolved files
After choosing versions, add both files to the staging area withgit add config.yaml README.mdto mark conflicts resolved.Final Answer:
git checkout --ours config.yaml && git checkout --theirs README.md && git add config.yaml README.md -> Option AQuick Check:
Use checkout --ours/theirs per file, then git add [OK]
- Mixing up --ours and --theirs for files
- Forgetting to git add after checkout
- Using git reset or git merge incorrectly
