1
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?

More than 3 years have passed since last update.

【Git】あるPRから1部(中間)のコミットをmainに反映させたい

Last updated at Posted at 2021-09-17

すでにStagingにmerge済みコミットの中から、中間のコミットだけを即座にmainブランチにmergeさせたい場合があった。中間のコミットを1つだけプッシュするには、 git cherry-pick で行うことができる。

結論

まずmainに移って、ローカルmainとリモートmainの差異をなくす

$ git checkout main
$ git pull origin main

# ローカルブランチをリモートの状態に戻したい場合
$ git branch | grep '*'
* main
$ git reset --hard origin/main

全く新しいブランチを、新規で切る

$ git checkout -b new-branch master

mainに反映させたいコミットのハッシュで cherry-pick

$ git cherry-pick コミットのハッシュ

new-branch にpush

$ git push origin new-branch

=> GitHubのUIから、main <= new-branch のPRを作ってmainに向けて新しいPRを作成する

=> mainにmerge

説明

A - B - C [main]
         \
          D - E - F [feature]
mainから新しいブランチを作成

$ git checkout -b new-branch main

          [new-branch]
A - B - C [main]
         \
          D - E - F [feature]
(Cを指す新しいブランチラベルが作成された以外は何も変更されていないことに注意)

次に、Eをチェリーピックする。これにより、EがCの上に新しいコミットとしてコピーされる。競合が発生する可能性があるため通常どおり解決する。

$ git cherry-pick E

          E1 [new-branch]
         / 
A - B - C [main]
         \
          D - E - F [feature]

これでプッシュできnew-branch、E1のみがPRに含まれる。

https://qiita.com/muran001/items/dea2bbbaea1260098051
https://www.webdevqa.jp.net/ja/git/%E7%89%B9%E5%AE%9A%E3%81%AE%E3%82%B3%E3%83%9F%E3%83%83%E3%83%88%E3%82%92%E3%80%8C%E3%83%97%E3%83%AB%E3%83%AA%E3%82%AF%E3%82%A8%E3%82%B9%E3%83%88%E3%80%8D%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95/1056172386/
https://www.atlassian.com/ja/git/tutorials/cherry-pick#:~:text=git%20cherry%2Dpick%20%E3%81%AF%E5%A4%89%E6%9B%B4,%E5%A0%B4%E6%89%80%E3%81%AB%E4%BB%98%E3%81%91%E6%9B%BF%E3%81%88%E3%82%89%E3%82%8C%E3%81%BE%E3%81%99%E3%80%82
https://www.sejuku.net/blog/71544

1
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
1
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?