subブランチで長期間作業してmainブランチとの際が出てきたらmergeしとく

git
スポンサーリンク

問題点

subブランチで長期間作業をして、る間にmainブランチも頻繁にアップデートされてった場合、subブランチをmainブランチにマージするときコンフリクトだらけになりそうで怖い。

結論

ちょいちょいmainブランチをsubブランチに取り込む(merge)する。

  • git mergeと git rebase がある
  • git merge でいんじゃね

ポイント

  • git fetchが必要

以下パープレさんの回答

サブブランチでの作業が長期間にわたる場合、mainブランチの最新の変更をサブブランチに取り込むのが一般的です[1]。

これにより、いざサブブランチをmainにマージしようとした際の、大規模なコンフリクト(競合)を防ぐことができます[1]。

そのための代表的な方法が2つあります。git rebasegit merge です[2]。

方法 特徴 コミット履歴
git rebase 履歴が一本のきれいな線になる[3] マージコミットが作られず、あたかも最新のmainから作業を始めたかのように見える[3][2]。
git merge mainの変更を取り込んだ事実が「マージコミット」として履歴に残る[4] 誰がいつmainを取り込んだか明確だが、履歴は分岐と合流を繰り返して複雑になりがち[4]。

方法1:git rebase を使う(推奨されることが多い)

rebaseは、サブブランチの変更を、mainブランチの最新コミットの「上」に付け替える(re-base)操作です。これにより、コミット履歴が一本のきれいな直線になります[3]。

手順
1. リモートの最新情報を取得します[4]。
bash
git fetch origin

2. 現在のブランチに、origin/main の内容をrebaseします[4][2]。
bash
git rebase origin/main

3. もしコンフリクトが発生したら、ファイルを修正して git add し、git rebase --continue を実行します[4][2]。

  1. rebaseは履歴を書き換えるため、リモートにpushする際は --force が必要です[4][2]。
    bash
    git push -f origin your-sub-branch

方法2:git merge を使う

mergeは、mainブランチの変更を、サブブランチに「マージ(合流)」させる操作です。これにより、「mainをマージした」というマージコミットがサブブランチの履歴に残ります[4]。

手順
1. リモートの最新情報を取得します[4][2]。
bash
git fetch origin

2. 現在のブランチに、origin/main の内容をマージします[4][2]。
bash
git merge origin/main

3. もしコンフリクトが発生したら、ファイルを修正して git addgit commit を実行します[4]。

  1. 通常通りpushします[4]。
    bash
    git push origin your-sub-branch

どちらを使うべきか?

  • rebase を推奨するケース
    • 最終的にmainにマージする前に、履歴をきれいにしたい場合[2]。
    • 他の人が作業していない個人のフィーチャーブランチである場合[2]。
  • merge を推奨するケース
    • 「mainの変更をいつ取り込んだか」という事実を履歴として正確に残したい場合[2]。
    • 複数人で共有しているブランチで、履歴の書き換えを避けたい安全な運用をしたい場合[2]。

結論として、「長期間放置せず、こまめにmainの変更を取り込むこと」が、コンフリクトを最小限に抑えるための最も重要な習慣です[1]。

[1] https://github.com/orgs/community/discussions/161932
[2] https://www.geeksforgeeks.org/git/how-to-get-changes-from-master-into-a-branch-in-git/
[3] https://git-scm.com/book/en/v2/Git-Branching-Rebasing
[4] https://graphite.dev/guides/how-to-sync-git-branch-with-main
[5] https://stackoverflow.com/questions/31050944/what-is-best-practice-for-updating-feature-branches-to-a-new-base/31054857
[6] https://stackoverflow.com/questions/61069101/how-do-i-pull-changes-from-a-branch-in-master-to-a-subbranch
[7] https://qiita.com/nasuB7373/items/0d3c67777f3b53cbefb0
[8] https://www.reddit.com/r/github/comments/occ5ej/how_to_update_local_feature_branch_to_reflect_the/
[9] https://phoenixnap.com/kb/git-merge-master-into-branch
[10] https://moldstud.com/articles/p-keep-your-feature-branch-updated-with-main-branch-tips
[11] https://stackoverflow.com/questions/59916041/how-to-update-a-branch-with-master-on-github/59916074
[12] https://stackoverflow.com/questions/39851288/git-merge-with-sub-branches
[13] https://www.reddit.com/r/git/comments/185xcuw/is_there_a_way_to_rebase_a_branch_and_its_parent/
[14] https://codemia.io/knowledge-hub/path/update_git_branches_from_master
[15] https://www.youtube.com/watch?v=45uNJvo6djA
[16] https://www.atlassian.com/ja/git/tutorials/rewriting-history/git-rebase
[17] https://stackoverflow.com/questions/23748973/is-it-neccessary-to-update-git-branch-before-merging-it-to-develop-master
[18] https://www.varonis.com/blog/git-branching
[19] https://qiita.com/Mayumi_Pythonista/items/72c6b440b6a82c367e9b
[20] https://tracpath.com/docs/git-rebase/

コメント