0
0
Gitdevops~5 mins

Ours vs theirs in conflicts in Git - CLI Comparison

Choose your learning style9 modes available
Introduction
When two people change the same part of a file in Git, a conflict happens. You must choose which change to keep: yours or the other person's. This helps keep your project working smoothly.
When you pull changes from a shared repository and Git cannot merge automatically.
When you merge a feature branch into the main branch and both changed the same lines.
When you rebase your branch onto another branch and conflicts appear.
When you want to decide quickly which version of a file to keep during a conflict.
When you want to avoid manually editing conflict markers in files.
Commands
This tries to merge the feature-branch into your current branch. If both changed the same lines, Git will stop and show a conflict.
Terminal
git merge feature-branch
Expected OutputExpected
Auto-merging example.txt CONFLICT (content): Merge conflict in example.txt Automatic merge failed; fix conflicts and then commit the result.
This command keeps your current branch's version of example.txt when resolving the conflict.
Terminal
git checkout --ours example.txt
Expected OutputExpected
No output (command runs silently)
This marks the conflict in example.txt as resolved by adding the chosen version to the staging area.
Terminal
git add example.txt
Expected OutputExpected
No output (command runs silently)
This commits the merge with the conflict resolved by keeping your version of the file.
Terminal
git commit -m "Resolved conflict using our version"
Expected OutputExpected
[main 1a2b3c4] Resolved conflict using our version
This command keeps the other branch's version of example.txt when resolving the conflict.
Terminal
git checkout --theirs example.txt
Expected OutputExpected
No output (command runs silently)
Key Concept

If you remember nothing else from this pattern, remember: 'ours' means keep your current branch's changes, 'theirs' means keep the other branch's changes during a conflict.

Common Mistakes
Running 'git checkout --ours' or '--theirs' without adding the file afterwards.
Git still sees the conflict as unresolved until you add the file, so the merge cannot complete.
After choosing 'ours' or 'theirs', always run 'git add <file>' to mark the conflict resolved.
Confusing 'ours' and 'theirs' as meaning your local changes vs remote changes.
'Ours' and 'theirs' refer to branches involved in the merge, not local vs remote repositories.
Understand 'ours' is your current branch, 'theirs' is the branch being merged in.
Trying to use 'git checkout --ours' on files without conflicts.
This command only works on files with merge conflicts; otherwise, it does nothing or errors.
Use this command only when Git reports a conflict in the file.
Summary
Run 'git merge' to combine branches and detect conflicts.
Use 'git checkout --ours <file>' or 'git checkout --theirs <file>' to pick which version to keep.
Add the resolved file with 'git add <file>' and then commit the merge.