Git Ours and Theirs Strategies: Mastering Conflict Resolution

Learn how to use Git's ours and theirs strategies to control merge behavior and resolve conflicts efficiently. Discover when and how to use these powerful tools for different scenarios.

Know More Team
January 27, 2025
4 min read
GitMerge StrategiesConflict ResolutionOursTheirs

Git Ours and Theirs Strategies: Mastering Conflict Resolution

When Git encounters conflicts during merges, it doesn't always need your manual intervention. Sometimes you know exactly which version you want to keep, and sometimes you want to automate the decision-making process. That's where Git's ours and theirs strategies come in—powerful tools that give you precise control over how conflicts are resolved.

Understanding the Strategies

The ours Strategy: Keeping Your Version

The ours strategy tells Git to favor your current branch's changes, even when there are conflicts with the incoming branch.

git merge -s ours feature-branch

What it does:

  • Pretends to merge the branches
  • Keeps only your current branch's content
  • Marks the merge as complete without actually incorporating changes
  • Creates a merge commit for history tracking

The theirs Strategy: Accepting Their Version

While there's no direct -s theirs merge strategy, you can achieve the same result during conflict resolution:

git checkout --theirs conflicted_file.txt
git add conflicted_file.txt

What it does:

  • Discards your local version of the conflicted file
  • Uses the incoming branch's version instead
  • Resolves the conflict automatically

When to Use Each Strategy

Use ours Strategy When:

1. Rolling Back Problematic Changes

# You want to undo a hotfix that caused issues
git merge -s ours hotfix/broken-fix

2. Closing Dead Branches

# Merge a long-abandoned feature branch without its changes
git merge -s ours feature/abandoned-feature

3. Maintaining Stable State

# Keep your current stable version despite incoming changes
git merge -s ours experimental-branch

Use theirs Strategy When:

1. Accepting Incoming Changes

# During a rebase, accept all incoming changes
git checkout --theirs .
git add .
git rebase --continue

2. Overriding Local Modifications

# When you know the incoming version is correct
git checkout --theirs config.json
git add config.json

3. Resolving Repetitive Conflicts

# During a rebase with many similar conflicts
git checkout --theirs *.js
git add *.js
git rebase --continue

Practical Examples

Example 1: Rollback Scenario

Situation: A hotfix was applied but caused new issues. You want to roll it back while keeping the merge history.

# Current state: main branch has the problematic hotfix
# You want to revert to the state before the hotfix

# Create a rollback branch from the commit before the hotfix
git checkout -b rollback/hotfix-revert <commit-before-hotfix>

# Merge the hotfix branch using 'ours' strategy
git merge -s ours hotfix/problematic-fix

# This creates a merge commit that effectively undoes the hotfix
# while maintaining the history

Example 2: Configuration Override

Situation: You're rebasing a feature branch and want to accept all configuration changes from the main branch.

# During a rebase, you encounter conflicts in config files
git rebase main

# When conflicts occur, accept all config changes from main
git checkout --theirs config/
git add config/

# Continue with the rebase
git rebase --continue

Example 3: Feature Branch Cleanup

Situation: You have a feature branch that was abandoned, but you want to close it in the main branch.

# Merge the abandoned feature branch without its changes
git merge -s ours feature/abandoned-feature

# This closes the feature branch in the history
# without actually incorporating any of its changes

Advanced Usage Patterns

Pattern 1: Selective File Resolution

# Accept 'theirs' for specific files, keep 'ours' for others
git checkout --theirs package.json
git checkout --ours src/
git add .

Pattern 2: Batch Conflict Resolution

# Resolve all conflicts by accepting 'theirs'
git checkout --theirs .
git add .
git commit

Pattern 3: Merge with Custom Strategy

# Use 'ours' strategy for the merge
git merge -s ours feature-branch

# Then manually cherry-pick specific changes you want
git cherry-pick <specific-commit-hash>

Best Practices and Considerations

1. Understand the Impact

  • ours strategy creates a merge commit but doesn't incorporate changes
  • theirs strategy completely replaces your changes
  • Both strategies affect the commit history

2. Use with Caution

  • Always review what you're about to do
  • Test the result after using these strategies
  • Communicate with your team about significant decisions

3. Document Your Decisions

# Add a clear commit message explaining your choice
git commit -m "Merge feature-branch using 'ours' strategy to maintain stable state"

4. Consider Alternatives

  • Manual resolution for complex conflicts
  • Interactive rebase for more control
  • Cherry-picking for selective changes

Common Pitfalls and Solutions

Pitfall 1: Using ours When You Want to Merge

Problem: Using ours strategy when you actually want to incorporate changes Solution: Use regular merge or rebase instead

Pitfall 2: Using theirs Without Review

Problem: Accepting all incoming changes without understanding them Solution: Review changes before using theirs strategy

Pitfall 3: Forgetting to Stage Changes

Problem: Using theirs but forgetting to stage the resolved files Solution: Always run git add after using theirs

Pitfall 4: Misunderstanding the Strategies

Problem: Confusing ours and theirs with merge and rebase Solution: Remember that ours keeps your version, theirs accepts their version

Summary Table

StrategyCommandBehaviorUse When
oursgit merge -s ours branchKeeps current branch's contentRollbacks, closing dead branches
theirsgit checkout --theirs fileAccepts incoming branch's contentOverriding local changes, accepting incoming fixes

Conclusion

Git's ours and theirs strategies are powerful tools for controlling merge behavior and resolving conflicts efficiently. They're particularly useful when you know exactly which version you want to keep, or when you need to automate conflict resolution.

Remember:

  • ours keeps your version and creates a merge commit
  • theirs accepts their version and resolves conflicts
  • Use them wisely - they can significantly impact your codebase
  • Always test the result after using these strategies

Table of Contents

Navigate the scroll
Reading Progress