Choose Git merge strategy for specific files ours, mine, theirs
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When working collaboratively using Git, conflicts are an inevitable part of the version control process. A conflict arises when changes from different branches collide on the same file, and Git cannot automatically determine which version to keep. Choosing the right merge strategy can make resolving these conflicts more manageable, especially when dealing with specific files. This article delves into specific merge strategies — particularly "ours," "mine," and "theirs" — explaining when and how to use each effectively.
Understanding Git Merge Strategies
Git provides a variety of merge strategies to handle conflicts. Some of the commonly used strategies are:
- Merge Default: Involves automatically attempting to merge files and involves the use of the
git mergecommand. Conflicts are resolved manually. - Ours Strategy: This strategy is used when you want to keep the changes from the current branch and discard changes from the other branch.
- Theirs Strategy: The opposite of "ours," this strategy keeps the changes from the merged-in branch.
While "ours" and "theirs" are specific strategies, "mine" is a conceptual strategy discussed in similar contexts. It allows overwriting conflicted files with your version of the work tree’s state using checkout
.
Choosing the Right Strategy for Specific Files
The merge strategies allow granular control, especially when you need specific handling for individual files within a larger merge process.
Using "Ours"
The "ours" strategy is beneficial when:
- You prioritize the current branch's changes for a specific file or set of files, overriding changes from the other branch.
- The changes made in the other branch are experimental or unnecessary to the current progress.
Example:
- The changes in the merging branch need to override the current branch’s changes.
- Integrating the whole other team's update is necessary except for specific sections you're fully responsible for.
- Specialists managing specific features might need their modifications to take precedence in conflicting files.
- Selective Merge Control: Allows flexible conflict resolution on a file-by-file basis.
- Time Efficiency: Quickly resolve conflicts without manual line-level interventions for all files.
- Risk of Data Loss: You may unwittingly overwrite important changes when using the "ours" or "theirs" strategy thoughtlessly.
- Consistency Challenges: Inconsistent use across a merge may result in integration issues and overlooked dependencies.

