【Git】別のブランチから特定のコミットを取り込む

スポンサーリンク

はじめに

別のブランチのコミットを今のブランチに取り込む方法を紹介します。

別のブランチからコミットを取り入れる

取り込みたいコミットのあるブランチからgit logやGUIからコミットIDを取得します。

先ほど取得したコミットIDを利用して、取り込みたいブランチ側でgit cherry-pickをします。

git cherry-pick <commit-id>

実際にやってみる

実際にcherry-pickを使ってみます。

まずは、取り込まれる側のブランチ(branch-b)で取り込みたい変更をコミットしておきます。

❯ git checkout -b branch-b
Switched to a new branch 'branch-b'

❯ git add hoge.md

❯ git commit -m "branch-b commit"
[branch-b 5a07298] branch-b commit
 1 file changed, 1 insertion(+)

❯ git push origin branch-b

コミットIDをgit logから確認します。

❯ git log
commit 5a07298711b21b46a404bd0e1c69e8a6fbd6091e (HEAD -> branch-b, origin/branch-b)
Author: monda00
Date:   Thu Feb 10 21:43:54 2022 +0900

    branch-b commit

取り込みたい側のブランチに移動して、コミットIDを指定してcherry-pickします。

❯ git checkout branch-a
Switched to branch 'branch-a'

❯ git cherry-pick 5a07298711b21b46a404bd0e1c69e8a6fbd6091e
[branch-a 8de9ad1] branch-b commit
 Date: Thu Feb 10 21:43:54 2022 +0900
 1 file changed, 1 insertion(+)

確認するとコミットが取り込まれていることがわかります。

❯ git log
commit 8de9ad1c0722f37a4fe7030e01fc90bf718b5d6a (HEAD -> branch-a)
Author: monda00
Date:   Thu Feb 10 21:43:54 2022 +0900

    branch-b commit

参考

タイトルとURLをコピーしました