LoginSignup
33
40

More than 5 years have passed since last update.

git cherry-pickで助かりました(他ブランチのコミットを反映させる方法)

Last updated at Posted at 2017-11-02

前置き

つい最近、コミットするブランチを間違えてしまうというドジっ子な事をやってしまいました。そんな時に助けてくれたのが"git cherry-pick"さんです。

git cherry-pickとは?

git cherry-pickとは他のブランチの特定コミットを反映できる超絶便利なコマンドです。

git cherry-pick コミットID

で他のブランチのコミットを自分が今いるブランチに反映させる事が出来ます。
*インデックスに反映されます。つまりgit addした状態になるということです。コマンドを打った後はcommitしなくてはなりません。

コミットIDの見方

コミットIDをどう見ればいいのかわからないという方に説明しますと、

ターミナルでgit logを叩いてみます

commit 26151f9ee8......
Author: bossunn bossunn <test@test.co.jp>
Date:   Wed Nov 1 17:44:14 2017 +0900

    [change] テストやで〜

             Qiita書くやで〜

というlogがズラ~っと流れてくると思います。
一番上に書いてある

commit 26151f9ee8......

の26151f9ee8......がコミットIDとなります。

git cherry-pickについてわかったところで、次は利用していきましょう。

git cherry-pickの使い方

今いるブランチ(間違えてコミットしてしまったブランチ)をtest/cherryブランチ、
本来コミットするべきだったブランチをtestブランチとします。

$ git checkout test
Switched to branch test
$ git cherry-pick 26151f9ee8......
error: could not apply 26151f9ee8......
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'

コンフリクトが起こっています。
とりえあえずコンフリクトを解消しましょう。

コンフリクトを解消してから

ちゃんと、

git add ファイル名

をしてmodified:にうつしておきましょう。

最後は

git commit

をしましょう。これにて他ブランチのコミット内容が反映されたと思います。

参考URL様

https://www.backlog.jp/git-guide/stepup/stepup7_4.html
https://qiita.com/ta__ho/items/8204a22a53b02ee0817e
https://qiita.com/Rock22/items/e53d6135c59099149592

33
40
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
33
40