官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 宜丰县| 湟中县| 新沂市| 灌阳县| 巩义市| 团风县| 天镇县| 湟源县| 东乡县| 综艺| 遂宁市| 龙江县| 平定县| 永丰县| 晋宁县| 遂川县| 简阳市| 利津县| 新宁县| 贺兰县| 尼玛县| 喀喇沁旗| 安吉县| 富蕴县| 祥云县| 辉县市| 永登县| 余江县| 府谷县| 望江县| 盱眙县| 宣城市| 崇阳县| 依安县| 永平县| 卫辉市| 天门市| 五大连池市| 潼南县| 蓝田县| 行唐县|