Git cherry-pick でミスしたので、、、

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2680230/31b00de2-0025-0218-4a06-e226ca958d80.png)

# はじめに

git で `cherry-pick` コマンド打ったら、コンフリクトになって理由が？になりました。
忘れてるので、手を動かして思います。

# develop -> staging -> main ブランチの物語

1. create new branch "develop"

   - `> git switch -c develop`

1. add new file from "develop"

   - `> ni a.txt`
   - `+` a1

1. commit and push "develop" to origin

   - `> git push -u origin develop`

1. create new branch "staging" from "develop"

   - `> git switch -c staging`

1. push "staging" to origin

   - `> git push -u origin staging`

1. add new file from "develop"

   - `> git switch develop`
   - `> ni b.txt`
   - `+` b1

1. commit and push "develop" to origin

   - `> git push -u origin develop`

1. merge "develop" to "staging"

   - `> git switch staging`
   - `> git merge develop`
   - `> git push -u origin staging`

1. merge "staging" to "main"

   - `> git switch main`
   - `> git merge staging`
   - `> git push -u origin main`

1. create tag "main"

   - `> git tag -a v1.0 -m "first release"`
   - `> git push origin v1.0`

1. update file from "develop"

   - `> git switch develop`
   - a.txt, b.txt
   - `+` ab1

1. commit and push "develop" to origin

1. update file from "develop"

   - `> git switch develop`
   - a.txt, b.txt
   - `+` ab2

1. commit and push "develop" to origin

1. merge "develop" ("ab1" commit only) to "staging"

   - `> git log` from develop
   - update ab1 : 05810ac4784a45d48429a4b9e6edd61d23f42a6a
   - update ab2 : 10376018a0193a90b197ab5dac982ec48c53bc26
   - `> git switch staging`
   - `> git cherry-pick 05810ac4784a45d48429a4b9e6edd61d23f42a6a`
   - `> git push -u origin staging`

1. update file from "develop"

   - `> git switch develop`
   - a.txt, b.txt
   - `+` ab3

1. commit and push "develop" to origin

1. merge "develop" ("ab3" commit only) to "staging"

   - `> git log` from "develop"
     - update ab1 : 05810ac4784a45d48429a4b9e6edd61d23f42a6a
     - update ab2 : 10376018a0193a90b197ab5dac982ec48c53bc26
     - update ab3 : d754dd1c7bff0dc69bd12bd6f00662d43bcdf50b
   - `> git switch staging`
   - `> git reset --hard` (reset all from local staging branch)
   - `> git cherry-pick d754dd1c7bff0dc69bd12bd6f00662d43bcdf50b` ab3
   - `error: could not apply d754dd1... update ab3`
   - `hint: After resolving the conflicts, mark them with`
   - `...`
   - `> git cherry-pick --abort` (abort cherry-pick)
   - Q : why conflict
     - A : ab3 in ab2
   - `> git cherry-pick 10376018a0193a90b197ab5dac982ec48c53bc26` ab2
   - `> git push -u origin staging` (success ab2)

1. create "feature" branch at update file from "develop"

   - `> git switch develop`
   - `> git switch -c feature/update-ab4`
   - a.txt, b.txt
   - `+` ab4

1. commit and push "feature/update-ab4" to origin

1. merge "feature/update-ab4" to "develop"

   - `> git switch develop`
   - `> git merge feature/update-ab4`
   - `> git push -u origin develop`

1. merge "feature/update-ab4" ("ab4" commit only) to "staging"

   - `> git switch feature/update-ab4`
   - `> git log` from "feature/update-ab4"
     - update ab4 : dc4ce3a3ad16b70e267b53ab1f496de34da8baa6
   - `> git switch staging`
   - `> git cherry-pick dc4ce3a3ad16b70e267b53ab1f496de34da8baa6`
     - `Auto-merging a.txt`
     - `CONFLICT (content): Merge conflict in a.txt`
     - `Auto-merging b.txt`
     - `CONFLICT (content): Merge conflict in b.txt`
     - `error: could not apply dc4ce3a... update ab4`
     - `hint: After resolving the conflicts, mark them with`
     - `...`
     - `> git cherry-pick --abort` (abort cherry-pick)

1. create "feature" branch at update file from "develop"

   - `> git switch develop`
   - `> git switch -c feature/update-ab5`

1. create "feature" branch at update file from "develop"

   - `> git switch develop`
   - `> git switch -c feature/update-ab6`

1. add "ab5" to file from "feature/update-ab5"

   - `> git switch feature/update-ab5`
   - a.txt, b.txt
   - `+` ab5

1. commit and push "feature/update-ab5" to origin

1. add "ab6" to file from "feature/update-ab5"

   - `> git switch feature/update-ab6`
   - a.txt, b.txt
   - `+` ab6

1. commit and push "feature/update-ab6" to origin

1. merge "develop" to "staging"

   - `> git switch staging`
   - `> git merge develop`
     - `Auto-merging a.txt`
     - `CONFLICT (content): Merge conflict in a.txt`
     - `Auto-merging b.txt`
     - `CONFLICT (content): Merge conflict in b.txt`
     - `Automatic merge failed; fix conflicts and then commit the result.`
   - manual merge to a.txt, b.txt
   - `> git commit -m "conflict merge"`
   - `> git push -u origin staging`

1. merge "feature/update-ab5" to "develop"

   - `> git switch develop`
   - `> git merge feature/update-ab5`
   - `> git push -u origin develop`

1. merge "feature/update-ab6" to "develop"

   - `> git switch develop`
   - `> git merge feature/update-ab6`
     - `Auto-merging a.txt`
     - `CONFLICT (content): Merge conflict in a.txt`
     - `Auto-merging b.txt`
     - `CONFLICT (content): Merge conflict in b.txt`
     - `Automatic merge failed; fix conflicts and then commit the result.`
   - manual merge to a.txt, b.txt
   - `> git commit -m "conflict merge"`
   - `> git push -u origin develop`

1. merge "feature/update-ab6" to "staging" (cherry-pick)

   - `> git switch feature/update-ab6`
   - `> git log` from "feature/update-ab6"
     - update ab6 : b55f83731a03421a245b3268324e90c9aa223950
   - `> git switch staging`
   - `> git cherry-pick b55f83731a03421a245b3268324e90c9aa223950` ab6
   - `git log`
     - `+` update ab6
   - `> git push -u origin staging`

1. merge "staging" to "main"

   - `> git switch main`
   - `> git merge staging`
   - `> git push -u origin main`

1. create tag "main"

   - `> git tag -a v1.0.1 -m "bug fix ab6"`
   - `> git push origin v1.0.1`

1. merge "develop" to "staging"

   - `> git switch staging`
   - `> git merge develop`
     - `Auto-merging a.txt`
     - `CONFLICT (content): Merge conflict in a.txt`
     - `Auto-merging b.txt`
     - `CONFLICT (content): Merge conflict in b.txt`
     - `Automatic merge failed; fix conflicts and then commit the result.`
   - manual merge to a.txt, b.txt
   - `> git commit -m "conflict merge"`
   - `> git push -u origin staging`

1. merge "staging" to "main"

   - `> git switch main`
   - `> git merge staging`
   - `> git push -u origin main`

1. create tag "main"

   - `> git tag -a v1.0.2 -m "bug fix ab5"`
   - `> git push origin v1.0.2`

1. add README.md to "develop"

   - `> git switch develop`

1. commit and push "develop" to origin

END

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2680230/453a5f77-12ba-1f60-4f21-63472d813a8d.png)

# おわりに

読むだけじゃなくて、手を動かして体感する事って大事ですね。

---

**参考（感謝）**

- [GitHub](https://github.com/mt-magic/git_cherry-pick)
- [git cherry-pickで助かりました(他ブランチのコミットを反映させる方法)](https://qiita.com/bossunn24/items/dedb620541d852327934)
- [gitでコミットする前の変更を取り消す方法](https://qiita.com/tatetate55/items/ddacf1d85d858935f3bd)
- [Gitでの変更を戻す方法（初心者から中級者まで）](https://qiita.com/ys-office-llc/items/2a65193fd19894d8e4df)
- [【Git】cherry-pickで解決]()