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

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.

主站蜘蛛池模板: 尤溪县| 安远县| 潮安县| 怀柔区| 牡丹江市| 温州市| 安吉县| 壶关县| 贡嘎县| 楚雄市| 安远县| 黔西县| 壤塘县| 蕲春县| 银川市| 三亚市| 镇平县| 桐庐县| 长白| 枞阳县| 丹巴县| 镇赉县| 永丰县| 临泉县| 合肥市| 二手房| 天全县| 四会市| 满洲里市| 鲁甸县| 特克斯县| 株洲县| 大竹县| 斗六市| 横山县| 滨海县| 永川市| 修武县| 苗栗县| 星子县| 九龙坡区|