Git ours vs. git theirs
In Git, the terms ours
and theirs
are used to refer to the two conflicting versions of a file during a merge or rebase. Understanding which version is which is critical for resolving conflicts correctly [1].
The Meaning of ours
and theirs
The meaning of these terms depends on the specific Git command you are using.
During a Merge
When you perform a standard merge, the terms are straightforward:
ours
refers to the version of the file on the current branch—the one you are merging into.theirs
refers to the version of the file on the branch you are merging in—the one whose changes you are pulling.
Example:
Imagine you are on a main
branch and you want to merge changes from a feature-a
branch.
git checkout main
git merge feature-a
If a conflict occurs, git checkout --ours <file>
will keep the version from main
, while git checkout --theirs <file>
will keep the version from feature-a
[1].
During a Rebase
With git rebase
, the meaning is reversed, which can be a source of confusion. Rebase works by taking commits from your current branch and replaying them on top of the target branch.
ours
refers to the version of the file on the target branch (the one you are rebasing onto). This is the new, shared history.theirs
refers to the version of the file on the current branch (the one with the commits being reapplied). These are the changes being integrated into the new history.
Example:
Imagine you are on feature-a
and you want to rebase it onto main
.
git checkout feature-a
git rebase main
If a conflict occurs, git checkout --ours <file>
will keep the version from main
, while git checkout --theirs <file>
will keep the version from feature-a
[2].
Usage and Merge Strategies
ours
and theirs
can be used in two main ways:
-
File-level Conflict Resolution: The most common use is to explicitly choose a version for a specific file during a conflict. After using
git checkout --ours
orgit checkout --theirs
, you mustgit add
the file to stage the resolution before continuing the merge or rebase. -
Merge Strategies: Git also offers specific merge strategies to handle conflicts automatically.
git merge -s ours <branch>
: This strategy resolves all conflicts by choosing the version from your current branch (ours
), effectively discarding all changes from the merged branch. It is a powerful, destructive command that should be used with caution [3].git merge -s recursive -X theirs <branch>
: The-X
option, short for--strategy-option
, is used with therecursive
strategy. It tells Git to automatically choose the "their" version for any conflicting hunks, but it will still merge non-conflicting changes from both branches [3].
Sources
- Atlassian. "Resolving Conflicts During a Merge".
https://www.atlassian.com/git/tutorials/using-branches/merge-conflicts
- Git. "Git Branching - Rebasing".
https://git-scm.com/book/en/v2/Git-Branching-Rebasing
- Git. "Git Merge Strategies".
https://git-scm.com/docs/git-merge#_merge_strategies