dackdive's blog

新米webエンジニアによる技術ブログ。JavaScript(React), Salesforce, Python など

[git]複数のcommitをまとめてcherry-pickする

ちょいメモ。

別ブランチのコミットを他のブランチにも適用するときに便利な cherry-pick コマンドですが、
複数のコミットをまとめて cherry-pick してしまいたい時がたまにあります。

そんなとき

$ git cherry-pick [cherry-pick の始点となるコミット]..[cherry-pick の終点となるコミット]

というように、A..B という指定のしかたでコミット A からコミット B までの一連のコミットを cherry-pick することが可能です。

が、注意点として、始点となるコミットは 実際に cherry-pick したいコミットの1つ前 を選ぶ必要があります。

以下、例。

$ git branch -a
* master

$ git log
commit 63ca256a5f806c8b237c3ff5c872debc0be12359
Author: zaki-yama <xxx@gmail.com>
Date:   Mon Jun 6 17:22:35 2016 +0900

    first commit

$ git checkout -b foo

# 略: foo ブランチ上でコミットを進める

$ git log
commit 738b8de617497b37bc47c06ce9fa9f676c256b61
Author: zaki-yama <xxx@gmail.com>
Date:   Mon Jun 6 17:23:39 2016 +0900

    commit 3

commit bee000f8a765367fe489302b064f856b2e1b259a
Author: zaki-yama <xxx@gmail.com>
Date:   Mon Jun 6 17:23:12 2016 +0900

    commit 2

commit d4baa931f1d2c3259c45109b880b9222a592c61f
Author: zaki-yama <xxx@gmail.com>
Date:   Mon Jun 6 17:22:59 2016 +0900

    commit 1

commit 63ca256a5f806c8b237c3ff5c872debc0be12359
Author: zaki-yama <xxx@gmail.com>
Date:   Mon Jun 6 17:22:35 2016 +0900

    first commit

$ git checkout master

# commit 1 ~ commit 3 までを master に cherry-pick したい
# 1) 始点に commit 1 を指定した場合
# conflict する...
$ git cherry-pick d4baa931f1d2c3259c45109b880b9222a592c61f..738b8de617497b37bc47c06ce9fa9f676c256b61
error: could not apply bee000f... commit 2
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'

# 2) 始点に commit 1 の1つ前を指定した場合
# 正常に cherry-pick できる
$ git cherry-pick 63ca256a5f806c8b237c3ff5c872debc0be12359..738b8de617497b37bc47c06ce9fa9f676c256b61
[master 2326872] commit 1
 Date: Mon Jun 6 17:22:59 2016 +0900
 1 file changed, 1 insertion(+)
[master 85d0cd2] commit 2
 Date: Mon Jun 6 17:23:12 2016 +0900
 1 file changed, 1 insertion(+)
[master 3299797] commit 3
 Date: Mon Jun 6 17:23:39 2016 +0900
 1 file changed, 1 insertion(+)

$ git log
commit 3299797bf2964b6c728eb268255e34eee5877dd0
Author: zaki-yama <xxx@gmail.com>
Date:   Mon Jun 6 17:23:39 2016 +0900

    commit 3

commit 85d0cd22f4d962b8b6c670dd0894874049437343
Author: zaki-yama <xxx@gmail.com>
Date:   Mon Jun 6 17:23:12 2016 +0900

    commit 2

commit 2326872f0c80c9b2dd83200ba039aba0ceef9b9f
Author: zaki-yama <xxx@gmail.com>
Date:   Mon Jun 6 17:22:59 2016 +0900

    commit 1

commit 63ca256a5f806c8b237c3ff5c872debc0be12359
Author: zaki-yama <xxx@gmail.com>
Date:   Mon Jun 6 17:22:35 2016 +0900

    first commit