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

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.

主站蜘蛛池模板: 中宁县| 三河市| 达拉特旗| 阳江市| 花垣县| 阜宁县| 陇南市| 郴州市| 延津县| 庆元县| 平定县| 太白县| 巴塘县| 嘉荫县| 海原县| 吉林省| 吉隆县| 长葛市| SHOW| 资阳市| 讷河市| 南澳县| 隆子县| 务川| 长治市| 南汇区| 五河县| 五大连池市| 文山县| 长岭县| 建平县| 桑植县| 德阳市| 陇南市| 武威市| 鲁甸县| 和田县| 丹寨县| 阿合奇县| 出国| 上林县|