LoginSignup
4
0

More than 3 years have passed since last update.

git merge のコンフリクト時に使う git checkout --ours と --theirs の位置づけを調べてみた

Posted at

はじめに

git merge するときにあるファイルが複数箇所コンフリクトしてしまったとき、マージさせたブランチか、またはもともとチェックアウトしているブランチどちらかの内容を全面的に適用したいと思ったときに調べたメモです。

tl;dr

以下の使い分けで実現できました。

# チェックアウトしているブランチを反映したい場合
git checkout --ours <file>

# マージさせたブランチを反映したい場合
git checkout --theirs <file>

ours と theirs の位置づけ

git-checkout Documentation を確認すると以下となっています。以下引用します。

--ours
--theirs
When checking out paths from the index, check out stage #2 (ours) or #3 (theirs) for unmerged paths.

ours が stage #2、 theirs が stage #3 とのこと。

stage #2、stage #3 が何かは、gitrevisions Documentation に書いてありました。以下引用します。

:[<n>:]<path>, e.g. :0:README, :README
A colon, optionally followed by a stage number (0 to 3) and a colon, 
followed by a path, names a blob object in the index at the given path. 
A missing stage number (and the colon that follows it) names a stage 0 entry. During a merge, stage 1 is the common ancestor, 
stage 2 is the target branch’s version (typically the current branch),
and stage 3 is the version from the branch which is being merged.

ざっと読む限り以下のような番号割り振りになっているようでした。

  • stage #0:不足しているステージ番号を表す
  • stage #1:共通の祖先ブランチ
  • stage #2:マージ先のブランチ(形式的に現在のブランチ)
  • stage #3:マージしようとしているブランチ

おわりに

今回のことを調べるために git のマニュアルを読んでみたのですが、かなり奥深さを感じました。やっぱり一次情報は大切ですね。

参考

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0