git stash pop でコンフリクトが発生した時に stash の方を採用するには
以下のようなシナリオを想定してください。
A氏が最初のコミットをして、中央リポジトリにpush:
% echo 'ABC' > test.txt
% git add test.txt
% git commit -m '1st commit'
% git push
B氏が2番目のコミットをして、中央リポジトリにpush:
% git fetch
% git merge
% echo 'CBA' > test.txt
% git add test.txt
% git commit -m '2nd commit'
% git log --oneline
035b3d2 2nd commit
496cc84 1st commit
その間にA氏が同じファイルを編集。B氏の編集をマージせずに、自分の編集内容をstash:
% echo 'ACB' > test.txt
% git fetch
% git stash
% git stash list
stash@{0}: WIP on master: 496cc84 1st commit
A氏がB氏の編集をマージして、stashをpopするとコンフリクト発生:
% git merge
% git stash pop
Auto-merging test.txt
CONFLICT (content): Merge conflict in test.txt
% cat test.txt<<<<<<< Updated upstream
CBA
=======
ACB
>>>>>>> Stashed changes
ここで問題。test.txtを手動で編集せずに、A氏がstashの内容を作業ツリーのtest.txtに上書きするにはどうすればよいでしょうか?
答え:
% git checkout --theirs test.txt
直前のマージが衝突を起こしてまだ解決されていない時、ステージングエリアには“両側”の内容が記録されています。こちら側の内容を作業ツリーに持ってくるには--oursオプション、あちら側の内容を持ってくるには--theirsオプションを付けてgit checkoutコマンドを実行します。
git stash popでは、stashされた内容が「あちら側」になります。
ちなみに、コンフリクトが発生した直後の状態にtest.txtを戻したい場合は、次のコマンドを実行します。
% git checkout --merge test.txt