Deep Dive into Git Merge Strategies: Implementing -s theirs Equivalent Functionality

Nov 05, 2025 · Programming · 15 views · 7.8

Keywords: Git merge | Conflict resolution | Version control

Abstract: This article provides an in-depth exploration of the differences between -s ours and -s theirs strategies in Git merge operations, analyzing why Git doesn't natively support -s theirs strategy, and presents three practical implementation approaches. Through detailed examination of -X theirs option mechanism, file deletion conflict handling, and complete solutions based on temporary branches, it helps developers understand Git's internal merge principles and master best practices for conflict resolution. The article combines specific code examples and operational steps to provide practical guidance for team collaboration and version management.

Overview of Git Merge Strategies

In distributed version control systems, branch merging is one of the most fundamental operations. Git provides various merge strategies to meet different development needs, where -s ours and -X ours are frequently used options, but they differ fundamentally in implementation mechanisms.

Core Differences Between -s ours and -X ours

git merge -s ours employs a special merge strategy that completely ignores all changes from the merged branch, directly using the current branch's code tree as the merge result. This strategy doesn't generate any conflicts because Git doesn't compare content differences between the two branches at all. Its typical application scenario is when branch structure needs preservation while discarding code changes from specific branches.

In contrast, git merge -X ours is an option added to standard recursive merging. Git normally executes the three-way merge algorithm, comparing differences between common ancestors, current branch, and merged branch. Only when conflicts are detected does it prioritize the current branch's version as the resolution. This strategy preserves non-conflicting changes while favoring specific branches only during conflict resolution.

Working Mechanism of -X theirs Option

Although Git doesn't provide native -s theirs strategy, the -X theirs option can achieve similar effects in most scenarios. The execution flow of this option is as follows:

git checkout branchA
git merge -X theirs branchB

In this operation, Git performs standard recursive merging, but when file content conflicts are detected, it automatically selects branchB's version as the resolution. The advantage of this approach is preserving non-conflicting changes from both branches while employing bias strategy only when decisions are truly needed.

Special Handling of File Deletion Conflicts

The -X theirs option has limitations when handling file deletion operations. If branchB deletes a file while branchA modifies the same file, a delete/modify conflict occurs during merging. Manual handling is required:

git rm {deleted-file-name}

This step ensures file deletion operations execute correctly, avoiding residual conflicts. In actual development, it's recommended to check file status differences between two branches before merging, pre-handling potential deletion conflicts.

Complete Solution Based on Temporary Branches

For scenarios requiring complete simulation of -s theirs behavior, a complete solution based on temporary branches can be adopted. This method combines multiple Git commands to achieve complete adoption of the merged branch's code tree:

# Ensure current location on target branch
git checkout branchA

# Execute ours merge, creating merge commit but retaining current branch content
git merge -s ours branchB

# Create temporary branch pointing to merge commit
git branch branchTEMP

# Reset working directory and index to branchB's state
git reset --hard branchB

# Soft reset back to temporary branch, preserving working directory and index content
git reset --soft branchTEMP

# Modify merge commit content to branchB's code tree
git commit --amend

# Clean up temporary branch
git branch -D branchTEMP

# Verify merge result
git diff HEAD branchB

The advantage of this solution lies in completely preserving the merge commit's parent relationships while ensuring the final code tree matches the merged branch. Although involving multiple steps, it provides behavior closest to the -s theirs strategy.

Analysis of Practical Application Scenarios

In team collaborative development, selecting appropriate merge strategies is crucial. When forced adoption of specific branch code versions is needed, corresponding solutions should be chosen based on specific requirements:

For most conflict resolution scenarios, -X theirs provides good balance, resolving conflicts while preserving valuable changes. When complete overwriting of current branch content is needed, the temporary branch-based solution offers the most comprehensive approach.

In continuous integration environments, automation scripts can encapsulate these operations, ensuring consistency and repeatability of merge behaviors. Establishing clear merge strategy specifications within teams is recommended to reduce problems caused by merge operations.

Best Practice Recommendations

Based on actual project experience, we recommend the following best practices: thoroughly test compatibility between two branches before merging, use git diff to pre-check potential conflicts. For important merge operations, conduct testing in feature branches first, then merge to main branches after confirmation.

In team collaboration, establish clear code review processes to ensure rationality and safety of merge operations. Regularly provide team members with Git advanced operation training to enhance overall version control management levels.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.