LoginSignup
2
0

More than 5 years have passed since last update.

Git Conflict発生!片方のブランチのファイルを強制的に優先とする

Posted at

普通Conflict発生となったら、新旧両方のファイルを開いて、おとなしく手動でマージするもんですが、どうしても片方のファイルでまるっと上書きしたい時があったりします。

私が遭遇したのは、以下のような状況です。
1. fileA.txtをbranchAで作成
2. masterにbranchAをmerge
3. masterでfileA.txtを削除
4. branchAでfileA.txtを修正
5. branchAをmasterにmergeしようとすると 「fileA.txtはmasterで削除されたファイルです。CONFLICTですよ。」で拒否られる。

...まぁ手違いとはいえ、上のような混乱状況を作った自分が悪いとは言え、masterにはなく、branchAにしかないファイルをどうやってマージするの?単にbranchAのファイルが絶対優先で上書きマージしてほしいだけなのよ。

以下、そのような場合の手順です。

  1. git clone [マスタのURL] // まずはリポジトリ取り出します
  2. git fetch origin //
  3. git checkout -b branchA origin/branchA // branchAも取り出します
  4. git checkout master // masterに戻って
  5. git merge --no-ff branchA // これでCONFLICTのワーニング出ます
  6. git checkout --theirs fileA.txt // branchAのほうのfileA.txtが最優先なのだと宣言
  7. git add fileA.txt
  8. git commit
  9. git push

もしも逆にmaster側のほうが最優先なんだ!って場合は
6. git checkout --ours ..... らしい けどこっちの操作はやってないので確かなことが言えないです。

2
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
2
0