Git
merge strategies
version control
ours mine theirs
code collaboration

Choose Git merge strategy for specific files ours, mine, theirs

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

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:

  1. Merge Default: Involves automatically attempting to merge files and involves the use of the git merge command. Conflicts are resolved manually.
  2. Ours Strategy: This strategy is used when you want to keep the changes from the current branch and discard changes from the other branch.
  3. 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.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.