What does cherry-picking a commit with Git mean?

前端 未结 12 2131
梦毁少年i
梦毁少年i 2020-11-22 11:58

Recently, I have been asked to cherry-pick a commit.

So what does cherry-picking a commit in git mean? How do you do it?

相关标签:
12条回答
  • 2020-11-22 12:11

    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

    0 讨论(0)
  • 2020-11-22 12:14

    cherry-pick is a Git feature. If someone wants to Commit specific commits in one branch to a target branch, then cherry-pick is used.
    git cherry-pick steps are as below.

    1. checkout (switch to) target branch.
    2. git cherry-pick <commit id>
      

      Here commit id is activity id of another branch.Eg.

      git cherry-pick 9772dd546a3609b06f84b680340fb84c5463264f
      
    3. push to target branch

    Visit https://git-scm.com/docs/git-cherry-pick

    0 讨论(0)
  • 2020-11-22 12:14

    You can think if a cherry pick as similar to a rebase, or rather it's managed like a rebase. By this, I mean that it takes an existing commit and regenerates it taking, as the starting point, the head of the branch you're currently on.

    A rebase takes a commit that had a parent X and regenerates the commit as if it actually had a parent Y, and this is precisely what a cherry-pick does.

    Cherry pick is more about how you select the commits. With pull (rebase), git implicitly regenerates your local commits on top of what's pulled to your branch, but with cherry-pick you explicitly choose some commit(s), and implicitly regenerate it (them) on top of your current branch.

    So the way you do it differs, but under the hood they are very similar operations - the regeneration of commits.

    0 讨论(0)
  • 2020-11-22 12:14

    When you are working with a team of developers on a project, managing the changes between a number of git branches can become a complex task. Sometimes you don't want to merge a whole branch into another, and only need to pick one or two specific commits. This process is called 'cherry picking'.

    Found a great article on cherry picking, check it out for in-depth details: https://www.previousnext.com.au/blog/intro-cherry-picking-git

    0 讨论(0)
  • 2020-11-22 12:19

    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.

    0 讨论(0)
  • 2020-11-22 12:20

    Cherry picking in Git means to choose a commit from one branch and apply it onto another.

    This is in contrast with other ways such as merge and rebase which normally apply many commits onto another branch.

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

      git checkout master
      
    2. Execute the following:

      git cherry-pick <commit-hash>
      

    N.B.:

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

      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:

      git notes copy <from> <to>
      

    Additional links:

    • git official guide page
    0 讨论(0)
提交回复
热议问题