什么是 Git 遴选（cherry-pick）？
git 遴选

> 
> 了解 `git cherry-pick` 命令是什么，为什么用以及如何使用。
> 
> 
> 


![](/data/attachment/album/202104/14/131735o63v3ow6y2wc281o.jpg "Measuring and baking a cherry pie recipe")


当你和一群程序员一起工作时，无论项目大小，处理多个 Git 分支之间的变更都会变得很困难。有时，你不想将整个 Git 分支合并到另一个分支，而是想选择并移动几个特定的提交。这个过程被称为 “<ruby> 遴选 <rt>  cherry-pick </rt></ruby>”。


本文将介绍“遴选”是什么、为何使用以及如何使用。


那么让我们开始吧。


### 什么是遴选？


使用遴选（`cherry-pick`）命令，Git 可以让你将任何分支中的个别提交合并到你当前的 [Git HEAD](https://acompiler.com/git-head/) 分支中。


当执行 `git merge` 或者 `git rebase` 时，一个分支的所有提交都会被合并。`cherry-pick` 命令允许你选择单个提交进行整合。


### 遴选的好处


下面的情况可能会让你更容易理解遴选功能。


想象一下，你正在为即将到来的每周冲刺实现新功能。当你的代码准备好了，你会把它推送到远程分支，准备进行测试。


然而，客户并不是对所有修改都满意，要求你只呈现某些修改。因为客户还没有批准下次发布的所有修改，所以 `git rebase` 不会有预期的结果。为什么会这样？因为 `git rebase` 或者 `git merge` 会把上一个冲刺的每一个调整都纳入其中。


遴选就是答案！因为它只关注在提交中添加的变更，所以遴选只会带入批准的变更，而不添加其他的提交。


还有其他几个原因可以使用遴选：


* 这对于 bug 修复是必不可少的，因为 bug 是出现在开发分支中对应的提交的。
* 你可以通过使用 `git cherry-pick` 来避免不必要的工作，而不用使用其他选项例如 `git diff` 来应用特定变更。
* 如果因为不同 Git 分支的版本不兼容而无法将整个分支联合起来，那么它是一个很有用的工具。


### 使用 cherry-pick 命令


在 `cherry-pick` 命令的最简单形式中，你只需使用 [SHA](https://en.wikipedia.org/wiki/Secure_Hash_Algorithms) 标识符来表示你想整合到当前 HEAD 分支的提交。


要获得提交的哈希值，可以使用 `git log` 命令：



```
$ git log --oneline

```

当你知道了提交的哈希值后，你就可以使用 `cherry-pick` 命令。


语法是：



```
$ git cherry-pick <commit sha>

```

例如：



```
$ git cherry-pick 65be1e5

```

这将会把指定的修改合并到当前已签出的分支上。


如果你想做进一步的修改，也可以让 Git 将提交的变更内容添加到你的工作副本中。


语法是：



```
$ git cherry-pick <commit sha> --no-commit

```

例如：



```
$ git cherry-pick 65be1e5 --no-commit

```

如果你想同时选择多个提交，请将它们的提交哈希值用空格隔开：



```
$ git cherry-pick hash1 hash3

```

当遴选提交时，你不能使用 `git pull` 命令，因为它能获取一个仓库的提交**并**自动合并到另一个仓库。`cherry-pick` 是一个专门不这么做的工具；另一方面，你可以使用 `git fetch`，它可以获取提交，但不应用它们。毫无疑问，`git pull` 很方便，但它不精确。


### 自己尝试


要尝试这个过程，启动终端并生成一个示例项目：



```
$ mkdir fruit.git
$ cd fruit.git
$ git init .

```

创建一些数据并提交：



```
$ echo "Kiwifruit" > fruit.txt
$ git add fruit.txt
$ git commit -m 'First commit'

```

现在，通过创建一个项目的复刻来代表一个远程开发者：



```
$ mkdir ~/fruit.fork
$ cd !$
$ echo "Strawberry" >> fruit.txt
$ git add fruit.txt
$ git commit -m 'Added a fruit"

```

这是一个有效的提交。现在，创建一个不好的提交，代表你不想合并到你的项目中的东西：



```
$ echo "Rhubarb" >> fruit.txt
$ git add fruit.txt
$ git commit -m 'Added a vegetable that tastes like a fruit"

```

返回你的仓库，从你的假想的开发者那里获取提交的内容：



```
$ cd ~/fruit.git
$ git remote add dev ~/fruit.fork
$ git fetch dev
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (6/6), done...

```


```
$ git log –oneline dev/master
e858ab2 Added a vegetable that tastes like a fruit
0664292 Added a fruit
b56e0f8 First commit

```

你已经从你想象中的开发者那里获取了提交的内容，但你还没有将它们合并到你的版本库中。你想接受第二个提交，但不想接受第三个提交，所以使用 `cherry-pick`。



```
$ git cherry-pick 0664292

```

第二次提交现在在你的仓库里了：



```
$ cat fruit.txt
Kiwifruit
Strawberry

```

将你的更改推送到远程服务器上，这就完成了！


### 避免使用遴选的原因


在开发者社区中，通常不鼓励所以遴选。主要原因是它会造成重复提交，而你也失去了跟踪你的提交历史的能力。


如果你不按顺序地遴选了大量的提交，这些提交会被记录在你的分支中，这可能会在 Git 分支中导致不理想的结果。


遴选是一个强大的命令，如果没有正确理解可能发生的情况，它可能会导致问题。不过，当你搞砸了，提交到错误的分支时，它可能会救你一命（至少是你当天的工作）。




---


via: <https://opensource.com/article/21/4/cherry-picking-git>


作者：[Rajeev Bera](https://opensource.com/users/acompiler) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Whenever you're working with a group of programmers on a project, whether small or large, handling changes between multiple Git branches can become difficult. Sometimes, instead of combining an entire Git branch into a different one, you want to select and move a couple of specific commits. This procedure is known as "cherry-picking."

This article will cover the what, why, and how of cherry-picking.

So let's start.

## What is cherry-pick?

With the `cherry-pick`

command, Git lets you incorporate selected individual commits from any branch into your current [Git HEAD](https://acompiler.com/git-head/) branch.

When performing a `git merge`

or `git rebase`

, all the commits from a branch are combined. The `cherry-pick`

command allows you to select individual commits for integration.

## Benefits of cherry-pick


The following situation might make it easier to comprehend the way cherry-picking functions.

Imagine you are implementing new features for your upcoming weekly sprint. When your code is ready, you will push it into the remote branch, ready for testing.

However, the customer is not delighted with all of the modifications and requests that you present only certain ones. Because the client hasn't approved all changes for the next launch, `git rebase`

wouldn't create the desired results. Why? Because `git rebase`

or `git merge`

will incorporate every adjustment from the last sprint.

Cherry-picking is the answer! Because it focuses only on the changes added in the commit, cherry-picking brings in only the approved changes without adding other commits.

There are several other reasons to use cherry-picking:

- It is essential for bug fixing because bugs are set in the development branch using their commits.
- You can avoid unnecessary battles by using
`git cherry-pick`

instead of other options that apply changes in the specified commits, e.g.,`git diff`

. - It is a useful tool if a full branch unite is impossible because of incompatible versions in the various Git branches.

## Using the cherry-pick command

In the `cherry-pick`

command's simplest form, you can just use the [SHA](https://en.wikipedia.org/wiki/Secure_Hash_Algorithms) identifier for the commit you want to integrate into your current HEAD branch.

To get the commit hash, you can use the `git log`

command:

`$ git log --oneline`


Once you know the commit hash, you can use the `cherry-pick`

command.

The syntax is:

`$ git cherry-pick <commit sha>`


For example:

`$ git cherry-pick 65be1e5`


This will dedicate the specified change to your currently checked-out branch.

If you'd like to make further modifications, you can also instruct Git to add commit changes to your working copy.

The syntax is:

`$ git cherry-pick <commit sha> --no-commit`


For example:

`$ git cherry-pick 65be1e5 --no-commit`


If you would like to select more than one commit simultaneously, add their commit hashes separated by a space:

`$ git cherry-pick hash1 hash3`


When cherry-picking commits, you can't use the `git pull`

command because it fetches *and* automatically merges commits from one repository into another. The `cherry-pick`

command is a tool you use to specifically not do that; instead, use `git fetch`

, which fetches commits but does not apply them. There's no doubt that `git pull`

is convenient, but it's imprecise.

## Try it yourself

To try the process, launch a terminal and generate a sample project:

```
$ mkdir fruit.git
$ cd fruit.git
$ git init .
```


Create some data and commit it:

```
$ echo "Kiwifruit" > fruit.txt
$ git add fruit.txt
$ git commit -m 'First commit'
```


Now, represent a remote developer by creating a fork of your project:

```
$ mkdir ~/fruit.fork
$ cd !$
$ echo "Strawberry" >> fruit.txt
$ git add fruit.txt
$ git commit -m 'Added a fruit"
```


That's a valid commit. Now, create a bad commit to represent something you wouldn't want to merge into your project:

```
$ echo "Rhubarb" >> fruit.txt
$ git add fruit.txt
$ git commit -m 'Added a vegetable that tastes like a fruit"
```


Return to your authoritative repo and fetch the commits from your imaginary developer:

```
$ cd ~/fruit.git
$ git remote add dev ~/fruit.fork
$ git fetch dev
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (6/6), done...
```


```
$ git log –oneline dev/master
e858ab2 Added a vegetable that tastes like a fruit
0664292 Added a fruit
b56e0f8 First commit
```


You've fetched the commits from your imaginary developer, but you haven't merged them into your repository yet. You want to accept the second commit but not the third, so use `cherry-pick`

:

`$ git cherry-pick 0664292`


The second commit is now in your repository:

```
$ cat fruit.txt
Kiwifruit
Strawberry
```


Push your changes to your remote server, and you're done!

## Reasons to avoid cherry-picking

Cherry-picking is usually discouraged in the developer community. The primary reason is that it creates duplicate commits, but you also lose the ability to track your commit history.

If you're cherry-picking a lot of commits out of order, those commits will be recorded in your branch, and it might lead to undesirable results in your Git branch.

Cherry-picking is a powerful command that might cause problems if it's used without a proper understanding of what might occur. However, it may save your life (or at least your day job) when you mess up and make commits to the wrong branches.

## Comments are closed.