ネット上にあるドキュメント等を読んでみたけどよくわからないので、実際にやってみた。

git cherry-pick

実際にやってみた手順

リポジトリを作成


$ git init hoge
Initialized empty Git repository in /Users/foobar/hoge/.git/
 
$ cd hoge/

master ブランチに hello.txt を追加


$ echo "hello" >> hello.txt
 
$ git add hello.txt 
 
$ git commit -m "hello"
[master (root-commit) af2b7bc] hello
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 hello.txt

one_two_three ブランチを作成


$ git branch one_two_three
 
$ git checkout one_two_three
Switched to branch 'one_two_three'

one_two_three ブランチに one.txt を追加


$ echo "one" >> one.txt
 
$ git add one.txt 
 
$ git commit -m "one"
[one_two_three 55aadd2] one
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 one.txt

one_two_three ブランチに two.txt を追加


$ echo "two" >> two.txt
 
$ git add two.txt
 
$ git commit -m "two"
[one_two_three 1bfba0c] two
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 two.txt

one_two_three ブランチに three.txt を追加


$ echo "three" >> three.txt
 
$ git add three.txt 
 
$ git commit -m "three"
[one_two_three 4d0d92e] three
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 three.txt

one_two_three ブランチにはファイルが4つある状態


$ ls
hello.txt   one.txt   three.txt   two.txt

master ブランチに切り替えてみると、ファイルが1つだけある状態


$ git checkout master
Switched to branch 'master'
 
$ ls
hello.txt

git cherry-pick で two.txt 追加時のコミットだけ適用する


$ git cherry-pick 1bfba0c
[master deba3d9] two
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 two.txt
 
$ ls
hello.txt   two.txt

ちゃんと指定したコミットだけマージできた。場合によってはコンフリクトしそうなので、 git cherry-pick を使うときは気をつけたい。

tags: git

Posted by NI-Lab. (@nilab)