乐闻世界logo
搜索文章和话题

Git 中 cherry pick 的作用是什么?

8 个月前提问
5 个月前修改
浏览次数112

6个答案

1
2
3
4
5
6

cherry-pick 是 Git 版本控制系统中的一个功能,它的作用是允许用户选择一个或多个其他分支上的提交(commits),并将这些提交复制到当前分支上。这样做的主要原因通常是因为这些提交包含了对特定问题的修复或者是某个特定功能的实现,而这些是当前分支所需要的。

使用 cherry-pick 的情况通常包括:

  1. 修复紧急问题:当在一个分支上发现了一个紧急问题并且此问题已经在另一个分支上被解决了,可以使用 cherry-pick 将这个修复迅速应用到当前分支上,而不必合并整个分支的所有更改。

  2. 代码回滚:在某些情况下,如果一次提交导致了问题,而这个问题在后续的提交中得到了解决,可以使用 cherry-pick 来选择性地将修复的提交应用到当前分支上。

  3. 功能挑选:在开发过程中,可能某个功能先在一个分支上实现,但是需要将其应用到另一个分支上。如果这两个分支不方便直接合并,可以使用 cherry-pick 来单独挑选该功能的提交。

  4. 代码整理:有时在多个分支上可能有很多临时或实验性的提交,当需要清理历史或重新组织提交时,可以使用 cherry-pick 来选择性地将有效的提交整合到一个新的分支上。

使用 cherry-pick 的命令格式如下:

bash
git cherry-pick <commit-hash>

其中 <commit-hash> 是想要复制的提交的哈希值。

这里有一个具体的例子:假设我们有两个分支,mainfeature。在 feature 分支上有一个提交(假设哈希值是 abc1234),这个提交修复了一个严重的 bug。现在,我们希望将这个修复迅速应用到 main 分支上,但我们不想合并整个 feature 分支的所有更改。我们可以在 main 分支上执行以下命令:

bash
git checkout main git cherry-pick abc1234

这样,abc1234 这个提交就被复制到了 main 分支上,而 main 分支上的其他代码保持不变。这是一个高效且精确地管理代码更改的方式。然而,需要注意的是,如果在不同分支之间 cherry-pick 被复制的提交,可能会遇到冲突,这时就需要手动解决这些冲突。

2024年6月29日 12:07 回复

Cherry-picking in Git means choosing a commit from one branch and applying it to another.

This contrasts with other ways such as merge and rebase which normally apply many commits to another branch.

It's also possible to cherry-pick multiple commits but merge is the preferred way over cherry-picking.

  1. Make sure you are on the branch you want to apply the commit to.

    shell
    git switch master
  2. Execute the following:

    shell
    git cherry-pick <commit-hash>

N.B.:

  1. If you cherry-pick from a public branch, you should consider using

    shell
    git cherry-pick -x <commit-hash>

    This will generate a standardized commit message. This way, you (and your co-workers) can still keep track of the origin of the commit and may avoid merge conflicts in the future.

  2. If you have notes attached to the commit they do not follow the cherry-pick. To bring them over as well, You have to use:

    shell
    git notes copy <from> <to>

Additional links:

2024年6月29日 12:07 回复

This quote is taken from: Version Control with Git

Using git cherry-pick The command git cherry-pick commit applies the changes introduced by the named commit on the current branch. It will introduce a new, distinct commit. Strictly speaking, using git cherry-pick doesn’t alter the existing history within a repository; instead, it adds to the history. As with other Git operations that introduce changes via the process of applying a diff, you may need to resolve conflicts to fully apply the changes from the given commit . The command git cherry-pick is typically used to introduce particular commits from one branch within a repository onto a different branch. A common use is to forward- or back-port commits from a maintenance branch to a development branch.

shell
$ git checkout rel_2.3 $ git cherry-pick dev~2 # commit F, below

before:

before

after:

after

Also, here is a very nice in action video tutorial about it: Youtube: Introduction to Git cherry-pick

2024年6月29日 12:07 回复

Cherry picking in Git is designed to apply some commit from one branch into another branch. It can be done if you eg. made a mistake and committed a change into wrong branch, but do not want to merge the whole branch. You can just eg. revert the commit and cherry-pick it on another branch.

To use it, you just need git cherry-pick hash, where hash is a commit hash from other branch.

For full procedure see: http://technosophos.com/2009/12/04/git-cherry-picking-move-small-code-patches-across-branches.html

2024年6月29日 12:07 回复

I prepared step-by-step illustrations what cherry-pick does — and an animation of these illustrations (near the end).

  1. Before cherry-picking
    (we are going to do a cherry-pick of the commit L from the branch feature):

    An image showing the state of a Git repository before cherry-picking. On the master branch, there are five commits, A through E. A second branch, labeled 'feature', contains four commits and diverges at commit B. The 'feature' commits are labeled K through N, with L in the middle.


  1. Starting the command git cherry-pick feature~2
    (feature~2 is the 2nd commit before
    feature, i.e. the commit L):

    This image shows the result of a cherry-pick operation taking the L commit out of the feature branch, and placing it after the E commit on the master branch.


  1. After performing the command (git cherry-pick feature~2):

    The master branch now contains a commit, called L', or L-prime. The feature branch is unchanged.


The same animated:

An animation of the above steps.


Note:

The commit L' is from the user's point of view (commit = snapshot) the exact copy of the commit L.

Technically (internally), it's a new, different commit (because e.g. L contains a pointer to K (as its parent), while L' contains a pointer to E).

2024年6月29日 12:07 回复

Short example of situation, when you need cherry pick

Consider following scenario. You have two branches.

a) release1 - This branch is going to your customer, but there are still some bugs to be fixed.

b) master - Classic master branch, where you can for example add functionality for release2.

NOW: You fix something in release1. Of course you need this fix also in master. And that is a typical use-case for cherry picking. So cherry pick in this scenario means that you take a commit from release1 branch and include it into the master branch.

2024年6月29日 12:07 回复

你的答案