Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In what instance would git cherry-pick be needed instead of git merge?

Tags:

git

For moving an individual from one branch to another I realize that there are a few options in git. I have experimented with git merge and git cherry-pick but am failing to see when git cherry-pick is preferable.

My understanding is the following:

git merge <hash> moves the specified commit from one branch to the other preserving it as one commit.

git cherry-pick <hash> creates a copy of the commit in the second branch but it is separate with its own commit hash.

The first option seems preferable to me but what are the instances when cherry-pick would be preferred?

like image 562
markdorison Avatar asked Feb 09 '11 16:02

markdorison


People also ask

What is git cherry-pick used for?

git cherry-pick is a powerful command that enables arbitrary Git commits to be picked by reference and appended to the current working HEAD. Cherry picking is the act of picking a commit from a branch and applying it to another. git cherry-pick can be useful for undoing changes.

Should I merge or cherry-pick?

Cherry picking commits is useful when you need a specific change in multiple branches where merging other history is not desirable. This is a specific workflow outside of merging, because combining the history of two branches brings over more commits then you need.

What is difference between git cherry-pick and merge?

With the cherry-pick command, Git lets you incorporate selected individual commits from any branch into your current 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.

Can you cherry-pick a merge request?

You can cherry-pick merge requests from the same project, or forks of the same project, from the GitLab user interface: In the merge request's secondary menu, select Commits to display the commit details page. In the top right corner, select Options > Cherry-pick to show the cherry-pick modal.


1 Answers

Say you have a branch from master that has a bunch of commits. Maybe you made a change that is appropriate on master, but you don't want to bring in all of the changes (a small bug fix, for example, or the addition of a small feature). With git cherry-pick, you can grab only that commit from the other branch and bring it into master.

like image 152
mipadi Avatar answered Sep 28 '22 16:09

mipadi