Sub topic 10
Taking one side with ours and theirs
Some conflicts need no careful blending. One version is simply right, and the other should be dropped. Git has --ours and --theirs for these cases, and they exist at three levels: a single file, a single merge, and a whole branch history. This chapter covers all three and the mistakes that come with each.
It assumes you can already read conflict markers, run git status on an unfinished merge, and finish one with git add and git commit.
What "ours" and "theirs" mean in a merge
During git merge, the two sides are named by who is running the command:
- ours is the branch you have checked out, the one
HEADpoints to. - theirs is the branch you named in the command.
Take the conflict from the earlier chapters. You are on main with timeout = 30, and you run git merge feature, which has timeout = 60. Here main is ours and feature is theirs.
In the conflict markers, ours is the top section after <<<<<<< HEAD. Theirs is the bottom section before >>>>>>> feature. Git also stores both versions in the index as stages 2 and 3, which git ls-files -u shows. Stage 2 is ours and stage 3 is theirs. Stage 1 is the common ancestor.
Taking one side for a single file
To resolve one conflicted file with one side, check that side out and stage the file:
# keep the version from the branch you are on
git checkout --ours -- config.txt
git add config.txt
# or keep the version from the branch being merged
git checkout --theirs -- config.txt
git add config.txtOn Git 2.23 and later, git restore does the same thing:
git restore --theirs -- config.txt
git add config.txtAfter the last file is staged, run git commit to complete the merge.
The whole file is replaced
--ours and --theirs replace the entire file with that side's version. They do not only settle the conflicted hunks.
Suppose feature changed config.txt in two places. One change conflicts with main and the other does not. Running git checkout --ours -- config.txt throws away both changes from feature, including the clean one. Git does not warn you, and the file no longer contains anything from feature.
If you want to keep the clean changes and choose a side only for the conflicting hunks, use the merge option described below. Alternatively, edit the file by hand.
Undoing a mistake
If you check out the wrong side, you can restore the conflicted state of the file, markers included:
git checkout --merge -- config.txtThis re-creates the merge result from the three stages. It only works while the file is still unmerged. Once you have run git add, the stages are gone. If you resolve and stage the wrong way, use git checkout --merge -- config.txt before you commit, because it also works on files you have already staged in this merge. Do not rely on this after the commit. By then you need to inspect the history instead.
Taking one side for the whole merge
There are two different tools here, and people often mix them up. Both are spelled with "ours".
The `-X` option: prefer a side only where conflicts occur
git merge -X ours feature
git merge -X theirs feature-X passes an option to the default merge strategy. Git still does a normal three-way merge. Changes that do not conflict are taken from both branches. Only the hunks that would have conflicted are settled automatically in favour of the side you named.
This is useful when you trust one side for disputed lines but still want everything else. It is risky because the conflicts are resolved silently. You never see them, so you cannot check whether the chosen side was right.
The `-s ours` strategy: ignore the other branch entirely
git merge -s ours feature-s selects a different merge strategy. With ours, Git creates a merge commit whose content is exactly your current branch. Everything from feature is discarded, conflicting or not. The history still records that feature was merged. Git will therefore treat its commits as already included in later merges.
This is rare. A typical use is to retire an abandoned branch: you want its history to count as merged, but you do not want its changes.
| Command | Content from the other branch | Conflicts |
|---|---|---|
| `git checkout --theirs -- file` | Replaces that whole file | Resolved for that file |
| `git merge -X theirs feature` | Non-conflicting changes kept, conflicting hunks taken | Resolved silently |
| `git merge -s ours feature` | None | None, nothing is compared |
The -X flag takes an option name, and -s takes a strategy name. -X ours and -s ours have different results, so check which flag you typed.
Ours and theirs swap during a rebase
A rebase replays your commits one at a time on top of another branch. Git does this by checking out the target branch and applying your commits to it, so the roles reverse:
- ours is the branch you are rebasing onto, plus the commits already replayed.
- theirs is the commit being replayed, which is your own work.
git switch feature
git rebase main
# conflict in config.txt
git checkout --theirs -- config.txt # keeps YOUR feature commit's version
git add config.txt
git rebase --continueHere --theirs keeps the change you wrote on feature, and --ours keeps what is on main. This catches people out, because the words are the same as in a merge but the meaning is reversed.
The same swap applies to git rebase -X ours. It prefers the branch you are rebasing onto, which means it drops your conflicting changes. If you wanted your own to win, you need -X theirs.
When one side deleted the file
For delete/modify conflicts, one side removed the file and the other changed it. In the index there is no stage for the deleted side, so checking out that side fails with an error such as path 'config.txt' does not have our version.
To keep the deletion, remove the file. To keep the file, check out the side that has it:
git rm config.txt # accept the deletion
# or
git checkout --theirs -- config.txt # keep the file, if theirs still has it
git add config.txtBinary files cannot be merged line by line, so Git always reports a conflict and leaves your side in the working tree. Choosing a side with --ours or --theirs is the normal way to resolve them.
Check before you commit
Taking a side is fast, so verify the result:
- Run
git statusand confirm no paths remain under "Unmerged paths". - Run
git diff --cachedand read what the merge commit will contain. - Search for leftover marker lines with
git diff --check. - Build or run the tests.
Exercise
You are on feature and run git rebase main. Git stops with a conflict in config.txt. You want to keep the version you wrote on feature. Which command do you use, and why?
Show answer
git checkout --theirs -- config.txt, then git add config.txt and git rebase --continue. During a rebase, "theirs" is the commit being replayed, which is your own feature work, and "ours" is the main side you are rebasing onto.
Exercise
What is the difference between git merge -X theirs feature and git merge -s ours feature?
Show answer
-X theirs does a normal three-way merge and resolves only the conflicting hunks in favour of feature, keeping non-conflicting changes from both branches. -s ours takes none of feature's content and makes a merge commit with exactly the current branch's tree, while still recording feature as merged.