知識があやふやなままgit mergeでコンフリクトした際に使っていましたが、あやふやなままではまずい状況になって調べたので記録として残しておきます。
結論から言うと -Xours/Xtheirs では
- オートマージされる部分はオートマージ
- コンフリクトした部分はours/theirsで指定したほうが使われる。
といった挙動になります。これは、git merge
でコンフリクトした際に使う–ours/theirsと挙動が異なります。
試してみる
編集前
# test.txt
1
2
3
4
5
mainブランチで編集
# test.txt
1 main
2
3 main
4
5
developブランチで編集
# test.txt
1
2
3 develop
4
5 develop
マージ (mainにて作業)
~/test ❯❯❯ git merge develop
Auto-merging test.txt
CONFLICT (content): Merge conflict in test.txt
Automatic merge failed; fix conflicts and then commit the result.
~/test ❯❯❯ git diff
diff --cc test.txt
index ae287a1,3f9e539..0000000
--- a/test.txt
+++ b/test.txt
@@@ -1,6 -1,6 +1,10 @@@
# test.txt
-1
+1 main
2
++<<<<<<< HEAD
+3 main
++=======
+ 3 develop
++>>>>>>> develop
4
- 5
+ 5 develop
(3行目がコンフリクト。それはそう。)
一旦戻して(git merge –abort)、-Xoursを使ってマージ
~/test ❯❯❯ git merge -Xours develop
Auto-merging test.txt
Merge made by the 'recursive' strategy.
test.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
マージ完了!
test.txt
# test.txt
1 main
2
3 main
4
5 develop
なるほど。どっちも適用されている。なるほど。
コンフリクト時の–ours/theirs
–oursの挙動について、コンフリクトした状態でgit checkout --ours test.txt
すると、、
~/test ❯❯❯ git merge develop
Auto-merging test.txt
CONFLICT (content): Merge conflict in test.txt
Automatic merge failed; fix conflicts and then commit the result.
~/test ❯❯❯ git checkout --ours test.txt
Updated 1 path from the index
~/test ❯❯❯ git add test.txt
~/test ❯❯❯ git commit -m "merge"
[main 31205d3] merge
~/test ❯❯❯ cat test.txt
# test.txt
1 main
2
3 main
4
5
コンフリクトした際のcheckout –ours だと、mainの変更のみが適用されています!なんと!
まとめ
git merge -Xours/-Xtheirs
- コンフリクトした部分のみ 選択したブランチ。
- オートマージできる部分は自動でマージされる
-
git checkout --ours
(git merge 後)- 片側のブランチのみ適用
- オートマージされない
なるほど
Pingback: 別のブランチからコミットを取り込む方法 (cherry-pick) [Git] – Site-Builder.wiki