- Continuous Integration,Delivery,and Deployment
- Sander Rossel
- 459字
- 2021-07-02 15:42:17
Cherry picking
Another form of merging is cherry picking. With a cherry pick, you target a specific commit on one branch and merge just that one commit, as a new separate commit, to another branch. That means you do not have to merge an entire branch all at once.
It also means that when you do cherry pick all the commits on a branch, the branch still will not be marked as merged and so deleting it is only possible by forcing it:
git cherry-pick [commit id]
You can get the commit ID using git log and preferably git log --oneline. For example, I have added a reactor branch and created two commits. In the first commit, I added a reactor.txt file and in the second commit, I changed some text in that file. Now, on my master branch, I only want the reactor.txt file, but not the change. We can use git log branch-name to look for a commit on a specific branch:
git checkout master
git log reactor --oneline -2
6cc4c08 Changed the reactor.
710a366 Added the reactor file.
git cherry-pick 710a366
[master baefa8b] Added the reactor file.
Date: Sun Jan 29 14:51:23 2017 +0100
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 reactor.txt
Notice how the changes of the cherry picked commit are applied to your current working directory and committed with the cherry picked commit's message immediately. It is possible to not commit your cherry picks automatically using the -n switch:
git cherry-pick -n 710a366
This way, you can cherry pick multiple commits from different branches, make changes, and commit everything at once. Whatever you do, your cherry pick is a separate commit and not linked to the original commit in any way (except maybe your commit message). Whenever you decide to merge the branch later, you may encounter some conflicts.
Cherry picking by ID is a bit of a hassle. The IDs are not exactly easy to remember, let alone type. You can use various patterns to target a specific commit or commits (plural). For example, the branch-name~index pattern selects the commit on a specific branch on a specific index (beginning at 0 for the latest commit). The previous cherry pick would then look as follows:
git cherry-pick -n reactor~1
You can cherry pick multiple commits using IDs or patterns:
git cherry-pick -n 710a366 reactor~0
Make sure you apply the commits in the correct order. There are ways to cherry pick entire branches and multiple branches, but there are easier ways to do this as we will see.
If you are using Git GUI, or any other Git client, making a cherry pick is as easy as right-clicking on the commit you want and selecting Cherry pick or something along those lines.
- JBoss Weld CDI for Java Platform
- Mastering ServiceStack
- Oracle Exadata性能優化
- Learning Python Design Patterns(Second Edition)
- 零基礎學Java程序設計
- Go并發編程實戰
- 青少年學Python(第1冊)
- Clojure Reactive Programming
- JavaScript應用開發實踐指南
- JSP程序設計實例教程(第2版)
- Go語言開發實戰(慕課版)
- Building Business Websites with Squarespace 7(Second Edition)
- Python物理建模初學者指南(第2版)
- 原型設計:打造成功產品的實用方法及實踐
- 從零開始學算法:基于Python