This Git error occurs when you use `git checkout --ours` or `git checkout --theirs` without being in a merge conflict state or without specifying file paths. These options only work during active merge conflicts and require a pathspec to indicate which files to check out.
This error occurs when you try to use `git checkout --ours` or `git checkout --theirs` in an invalid context. The `--ours` and `--theirs` options are specifically designed for resolving merge conflicts by checking out one version of conflicted files. When Git reports "--ours/--theirs is incompatible without --merge", it means you're trying to use these conflict resolution options when there's no active merge in progress. These flags only make sense when Git has detected conflicting changes between branches and has placed the repository in a conflicted state with unmerged entries in the index. The error may also appear as "fatal: '--ours/--theirs' cannot be used with switching branches" or "fatal: '--ours/--theirs' needs the paths to check out" depending on your Git version. The underlying issue is the same: you need to be in a merge conflict state and specify which files to check out.
First, check if you actually have unmerged files that need conflict resolution:
# Check your repository status
git statusIf you're in a merge conflict, you'll see output like:
On branch main
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: src/config.js
both modified: README.mdIf you see "nothing to commit, working tree clean" or no mention of unmerged paths, you're not in a conflict state and cannot use --ours/--theirs.
The --ours and --theirs options require you to specify which files to check out. They cannot be used alone:
# INCORRECT - missing file path
git checkout --ours
git checkout --theirs
# CORRECT - specify the conflicted file(s)
git checkout --ours src/config.js
git checkout --theirs README.md
# Check out multiple files at once
git checkout --ours file1.js file2.js file3.js
# Use a glob pattern to resolve all conflicts in a directory
git checkout --theirs src/*
# Resolve all conflicts in the entire repository
git checkout --ours .
git checkout --theirs .After checking out with --ours or --theirs, remember to stage the resolved files:
git add src/config.jsIf you want to use --ours/--theirs but aren't in a conflict state, you need to initiate a merge first:
# Start a merge that may produce conflicts
git merge feature-branch
# If conflicts occur, now you can use --ours/--theirs
git checkout --ours conflicted-file.txt
git add conflicted-file.txt
git commitIf the merge completes without conflicts, there's nothing to resolve with --ours/--theirs. In that case, Git automatically combined the changes.
To force conflicts for testing or specific scenarios:
# Merge without auto-committing (even if no conflicts)
git merge --no-commit feature-branch
# You can still see both versions but won't have conflict markersIf you want to automatically resolve conflicts in favor of one side during the merge itself, use merge strategy options instead of checkout:
# Automatically prefer our version when conflicts occur
git merge -X ours feature-branch
# Automatically prefer their version when conflicts occur
git merge -X theirs feature-branchImportant distinction:
- -X ours / -X theirs are strategy OPTIONS (lowercase x) - they resolve conflicts automatically during merge
- -s ours is a strategy (lowercase s) - it ignores the other branch entirely
# Strategy option: merges but prefers ours on conflicts
git merge -X ours feature-branch
# Strategy: completely ignores their changes (rarely what you want)
git merge -s ours feature-branchDuring a rebase, you can also encounter situations where --ours/--theirs are needed. The same rules apply - you must be in a conflicted state:
# Start a rebase
git rebase main
# If conflicts occur during rebase, resolve them
git checkout --theirs conflicted-file.txt
git add conflicted-file.txt
git rebase --continueImportant: Ours/theirs are swapped during rebase!
During a rebase:
- --ours refers to the branch you're rebasing ONTO (e.g., main)
- --theirs refers to YOUR commits being replayed
This is opposite of merge where:
- --ours is your current branch
- --theirs is the incoming branch
This swap happens because rebase replays your commits on top of the target branch, making the target branch "ours" from Git's perspective.
In Git 2.23+, you can use git restore which has clearer syntax:
# Equivalent to: git checkout --ours file.txt
git restore --ours file.txt
# Equivalent to: git checkout --theirs file.txt
git restore --theirs file.txt
# Restore from a specific source
git restore --source=HEAD file.txt
git restore --source=feature-branch file.txtThe git restore command is specifically for restoring working tree files, making the intent clearer than the overloaded git checkout command.
# Show what would be restored (dry run)
git restore --ours --dry-run file.txt### Understanding Git's Index Stages
When a merge conflict occurs, Git stores multiple versions of each conflicted file in the index using "stages":
- Stage 0: Normal, non-conflicted entries
- Stage 1: Common ancestor version (base)
- Stage 2: "Ours" - current branch (HEAD) version
- Stage 3: "Theirs" - incoming branch version
The --ours option checks out stage 2, while --theirs checks out stage 3. You can see these stages with:
# Show all index entries including stages
git ls-files -s
# Show unmerged entries with stages
git ls-files -u### Git Version Differences
The exact error message has evolved across Git versions:
- Older versions: "fatal: '--ours/--theirs' cannot be used with switching branches"
- Git 2.x+: "error: --ours/--theirs is incompatible without --merge"
- Git 2.43+: May show different messages when checking out from a tree-ish
The change in Git 2.43 (released late 2023) tightened validation, and commands like git checkout 'stash@{0}' --theirs -- file that worked in 2.42 may now fail.
### The --merge Option
The --merge option (without ours/theirs) performs a three-way merge when switching branches with local modifications:
# Attempt three-way merge when switching branches
git checkout --merge other-branchThis is different from --ours/--theirs which work on already-conflicted files.
### Combining with --conflict
You can customize how conflicts are displayed using --conflict:
# Re-checkout with diff3 style markers (shows base version)
git checkout --conflict=diff3 file.txt
# Available styles: merge (default), diff3, zdiff3
git checkout --conflict=zdiff3 file.txt### Resolving All Conflicts At Once
# Accept all our versions
git checkout --ours -- .
git add .
# Accept all their versions
git checkout --theirs -- .
git add .Warning: This blindly accepts one side for ALL conflicts. Only use when you're certain you want to discard all changes from the other side.
### Cherry-pick and Revert Conflicts
These commands can also produce conflicts where --ours/--theirs apply:
# During cherry-pick conflicts
git cherry-pick abc123
# If conflicts, --theirs = the cherry-picked commit
git checkout --theirs file.txt
git add file.txt
git cherry-pick --continue
# During revert conflicts
git revert def456
# If conflicts, --theirs = the original state before the reverted commitwarning: BOM detected in file, this may cause issues
UTF-8 Byte Order Mark (BOM) detected in file
fatal: Server does not support --shallow-exclude
Server does not support --shallow-exclude
warning: filtering out blobs larger than limit
Git partial clone filtering large blobs warning
fatal: Server does not support --shallow-since
Server does not support --shallow-since in Git
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server