0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

特定コミットのみを反映する方法

0
Posted at

事象

  • main:本番環境ブランチ
  • develop:開発環境ブランチ
    • develop ブランチから feature ブランチを作成し、作業完了後に develop ブランチへマージ
    • 適切なタイミングで develop ブランチを main ブランチへマージし、本番環境へ反映

本番環境で問題が発生し、「develop ブランチにある特定のコミットだけを main ブランチへ反映したい」というケースがありました。develop ブランチをそのまま main にマージすると、未リリースのコミットまで取り込まれてしまうため、直接のマージは避ける必要があります。

対応方法

git cherry-pick を利用して、必要なコミットのみを main ブランチへ反映しました。

手順

1. ハッシュの確認

develop ブランチ上で、main に反映したいコミットのハッシュを確認します。

2. 緊急ブランチの作成

ローカルリポジトリを最新化し、main ブランチから緊急用ブランチを作成します。

git switch main
git pull
git switch -c <hotfix-branch>

3. cherry-pickの実行

cherry-pick を実行して必要なコミットを取り込みます。

git cherry-pick -m1 <commit-hash>
  • -m1 はマージコミットを cherry-pick する場合に必要なオプションです。通常のコミットでは不要です。
  • コンフリクトが発生した場合は適切に解消します。

4. リモートへの反映

緊急ブランチをリモートへ push し、main にマージします。

git push -u origin <hotfix-branch>

その後、GitHub 上でプルリクエストを作成し、main ブランチへマージします。

環境情報

  • git version 2.43.0
  • GitHub
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?