Sub topic 10
Taking One Side with ours and theirs
This chapter assumes you can run a merge, read conflict markers, and stage a file with git add. Sometimes the right fix for a conflict is to keep one version unchanged. Git has commands for that, and the words "ours" and "theirs" appear in several places with different meanings.
What ours and theirs mean in a merge
During git merge, the two sides are named by position:
- ours is the branch you are on, the one
HEADpoints to. - theirs is the branch you are merging in.
In the conflict from the earlier example, you are on main and merge feature. The timeout = 30 line is ours. The timeout = 60 line is theirs.
Taking one side for a single file
To replace a conflicted file with one side's complete version, use git checkout with --ours or --theirs, then stage the file:
git checkout --ours -- config.txt
git add config.txt
# or take the other branch's version
git checkout --theirs -- config.txt
git add config.txtStaging the file tells Git the conflict is resolved. Finish with git commit.
Choosing a side for the whole merge
The merge strategy option -X changes how Git settles conflicting hunks. It is different from the file-level commands above:
git merge -X ours feature
git merge -X theirs featureWith -X ours, Git still merges automatically wherever it can. Only the hunks that would conflict are settled in favor of our side. Changes that do not overlap are still merged from both branches.
Do not confuse this with -s ours. That is a separate strategy that records a merge but ignores all of the other branch's changes.
ours and theirs swap during a rebase
A rebase replays your commits one at a time on top of another branch. Git is applying your commits while sitting on the new base, so the labels flip:
- ours is the branch you are rebasing onto, such as
main. - theirs is the commit being replayed, which is your own work.
git switch feature
git rebase main
# keep your feature commit's version of the file
git checkout --theirs -- config.txt
git add config.txt
git rebase --continueHere --theirs keeps the version from your feature commit. In a merge, the same command would keep the version from the branch being merged in.
| Operation | ours | theirs |
|---|---|---|
| `git merge feature` on `main` | `main` (current branch) | `feature` |
| `git rebase main` on `feature` | `main` (new base) | your `feature` commit being replayed |
Check before you commit
After taking a side, confirm the result is what you intended. Open the file, or run git diff --cached to see what will be committed. The markers should be gone and the content should match the side you chose.
Exercise
You are on feature and run git rebase main. A conflict stops the rebase in config.txt. You want to keep the main version of the file. Which command do you run, and why?
Show answer
Run git checkout --ours -- config.txt, then git add config.txt and git rebase --continue. During a rebase, "ours" is the branch you are rebasing onto, which is main. "Theirs" is your own commit being replayed.
Exercise
What is the difference between git checkout --theirs -- file and git merge -X theirs feature?
Show answer
The checkout command replaces one whole conflicted file with the other side's version, including non-conflicting changes from our side. The -X theirs option applies to the whole merge, but it only settles hunks that actually conflict in favor of the other side. Non-overlapping changes from both branches are still combined.