PRのマージ先と作業ブランチのコンフリクトの解消
状態
PR内に以下が表示される
code:_
This branch has conflicts that must be resolved
Use the command line to resolve conflicts before continuing.
原因
mainの開発が進む(他の修正がマージされる)ことで自分の作業ブランチと同ファイルの変更が追加されたためコンフリクトした
PR作成時 → main とは矛盾なし
PR作成後 → 他の人の修正が main に入った(=mainが進んだ)
その修正が PR の修正と重なった → コンフリクト
対策
mainの状態がPR作成時よりも進んでいる → 作業ブランチにmainの最新を取り込み、強制pushすることで解消できる
code: (sh)
git checkout {作業ブランチ}
git pull --rebase origin main
git push origin {ブランチ名} --force-with-lease --force-if-includes
mergeベースの開発の場合は以下(mergeではforce pushは不要)
code: (sh)
git pull origin main
git checkout {作業ブランチ}
git merge main
git push -u origin {ブランチ名}
rebaseのイメージ
M1 を起点にPR作成 → M3 にrebaseしてpush
mainの最新の後ろに作業コミットをつなげるイメージ
rebase後にA、B、C のコミットIDは変更される
code: (md)
1. rebase前
main: M1 --- M2 --- M3
作業ブランチ: A --- B --- C
2. rebase
main: M1 --- M2 --- M3
作業ブランチ: A' --- B' --- C'
3. force push後
リモート: M1 --- M2 --- M3 --- A' --- B' --- C'
作業ブランチ: A' --- B' --- C'
push時のエラー
2回目以降のpushでmainが更新されていた場合にエラーが発生する
code: (sh)
git push origin @
To github.com:fjordllc/bootcamp.git ! rejected HEAD -> chore/fix-japanese-test-title (non-fast-forward) error: failed to push some refs to 'github.com:fjordllc/bootcamp.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
--force-with-lease --force-if-includes オプションで安全に強制pushできる
ポイント
mainの開発が進んだ分を作業ブランチに取り込み、pushすることでPRに反映している(pushは自分の変更を反映させるだけでは無い)
開発ルールによって最新化にmerge/rebaseどちらを利用するかが決まる
#Git
#GitHub