Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Resolving Git Conflicts Using Ours vs Theirs
📖 Scenario: You are working on a team project using Git. Sometimes, when two people change the same file, Git cannot decide which change to keep. This is called a conflict. You will learn how to use ours and theirs strategies to resolve these conflicts.
🎯 Goal: Learn how to create a conflict in Git and resolve it by choosing either your changes (ours) or the other person's changes (theirs).
📋 What You'll Learn
Create a Git repository with two branches
Make conflicting changes in the same file on both branches
Use Git commands to resolve conflicts choosing ours or theirs
Verify the final file content after conflict resolution
💡 Why This Matters
🌍 Real World
In team projects, conflicts happen when multiple people edit the same files. Knowing how to resolve conflicts quickly keeps the project moving smoothly.
💼 Career
Developers and DevOps engineers must handle merge conflicts daily. Mastering <code>ours</code> and <code>theirs</code> helps avoid mistakes and lost work.
Progress0 / 4 steps
1
Create a Git repository and initial file
Initialize a new Git repository called conflict-demo. Create a file named message.txt with the exact content Hello from main branch. Add and commit this file with the message Initial commit on main.
Git
Hint
Use git init to start the repo, echo to create the file, and git add plus git commit to save changes.
2
Create a branch and make conflicting changes
Create a new branch called feature. Switch to it. Change the content of message.txt to Hello from feature branch. Add and commit with the message Update message on feature. Then switch back to main and change message.txt content to Hello from main branch updated. Add and commit with the message Update message on main.
Git
Hint
Use git branch and git checkout to switch branches. Use echo to change file content and commit each change.
3
Merge feature into main and resolve conflict using ours
While on the main branch, run git merge feature to create a conflict. Then resolve the conflict by choosing your version (the main branch) using git checkout --ours message.txt. Add the resolved file and commit with the message Resolve conflict using ours.
Git
Hint
After merge conflict, use git checkout --ours to keep your branch's version, then add and commit.
4
Verify the final content of message.txt
Print the content of message.txt using cat message.txt to verify it shows Hello from main branch updated after resolving the conflict with ours.
Git
Hint
Use cat message.txt to see the file content in the terminal.
Practice
(1/5)
1. In a Git merge conflict, what does ours refer to?
easy
A. A backup copy of the file before the merge
B. The version of the file in the branch you are merging
C. The common ancestor version of the file
D. The version of the file in your current branch
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 D
Quick Check:
Ours = current branch version [OK]
Hint: Ours = your branch, theirs = merging branch [OK]
Common Mistakes:
Confusing 'ours' with 'theirs'
Thinking 'ours' means the common ancestor
Assuming 'ours' is a backup copy
2. Which Git command correctly chooses the 'theirs' version of a conflicted file named app.js?
easy
A. git checkout --ours app.js
B. git checkout --theirs app.js
C. git reset --theirs app.js
D. git merge --theirs app.js
Solution
Step 1: Identify the correct command to pick 'theirs'
The command to choose the 'theirs' version during conflict is git checkout --theirs <file>.
Step 2: Verify the file name usage
Using app.js after the command specifies the file to resolve.
Final Answer:
git checkout --theirs app.js -> Option B
Quick Check:
Use 'git checkout --theirs' to pick theirs [OK]
Hint: Use 'git checkout --theirs <file>' to pick theirs version [OK]
Common Mistakes:
Using 'git reset' instead of 'git checkout'
Confusing '--ours' with '--theirs'
Trying 'git merge --theirs' which is invalid
3. After a merge conflict on index.html, you run: git checkout --ours index.html What will be the content of index.html after this command?
medium
A. The version from your current branch before the merge
B. The version from the branch you are merging
C. The common ancestor version
D. An empty file
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.html will have your branch's original content, ignoring the other branch's changes.
Final Answer:
The version from your current branch before the merge -> Option A
Quick Check:
'git checkout --ours' = current branch content [OK]
Hint: Checkout --ours picks your branch's file version [OK]
Common Mistakes:
Thinking it picks the other branch's version
Assuming it resets to common ancestor
Expecting it to merge both versions automatically
4. You tried to resolve a conflict by running git checkout --theirs main.js but the file did not update. What is the likely cause?
medium
A. You need to commit before checking out
B. You used the wrong file name
C. You are not in a conflicted merge state
D. The command only works after resolving conflicts manually
Solution
Step 1: Check the merge state requirement
The git checkout --theirs command 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 C
Quick Check:
'git checkout --theirs' needs conflict state [OK]
Hint: Use 'git status' to confirm conflict before checkout --theirs [OK]
Common Mistakes:
Trying to use --theirs outside a merge conflict
Assuming checkout updates file without conflict
Not verifying current branch or file name
5. During a complex merge conflict involving multiple files, you want to keep your current branch's version for config.yaml but accept the other branch's version for README.md. Which sequence of commands correctly resolves this?
Step 1: Use correct commands to pick versions per file
To keep your branch's version for config.yaml, use git checkout --ours config.yaml. To accept the other branch's version for README.md, use git checkout --theirs README.md.
Step 2: Stage the resolved files
After choosing versions, add both files to the staging area with git add config.yaml README.md to mark conflicts resolved.