零弐壱蜂

[Git] コンフリクト時の --theirs / --ours の挙動

背景

Gitでコンフリクトが発生した際、--theirs--ours オプションを使用して、どの変更を採用するかを指定できる。

概要

  • --theirs--ours の意味はコマンドごとに異なる。
  • 代表的なケースとしてmerge, rebase, cherry-pick, revert を扱う。

1. merge

$ git checkout feature
$ git merge main

# main の内容を採用
$ git checkout --theirs [file]

# feature の内容を採用
$ git checkout --ours [file]

2. rebase

$ git checkout feature
$ git rebase main

# main の内容を採用
$ git checkout --ours [file]

# feature の内容を採用
$ git checkout --theirs [file]

3. cherry-pick

$ git checkout feature
$ git cherry-pick <commit_hash>

# cherry-pick するコミットの内容を採用
$ git checkout --theirs [file]

# feature ブランチの内容を採用
$ git checkout --ours [file]

4. revert

$ git revert <commit> <commit> ...

# リバートするコミットの内容を採用
$ git checkout --theirs [file]

# 現在のブランチの内容を採用
$ git checkout --ours [file]

コマンドごとの --theirs / --ours の意味

コマンド--theirs の内容--ours の内容
mergeマージ元(main)現在のブランチ(feature)
rebaseリベース先(main)現在のブランチ(feature)
cherry-pick適用するコミット現在のブランチ(feature)
revertリバートするコミット現在のブランチ