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?
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.
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.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With