Cherry Pick
CherryPick is used to select specific commits from one branch and apply them to another branch without merging the entire changes of the branch. This allows you to selectively introduce code changes with great precision, without merging the entire branch.
Its name comes from "cherry-picking," as you only pick the desired "fruit" (commits).
Use Cases
- Extracting Specific Features or Fixes from the Development Branch:
For example, if you completed an important fix in the dev branch but need to apply it to the main branch.
- Borrowing Code from Another Developer's Branch:
When another developer’s branch contains a commit that is useful to you, you can directly extract that commit into your branch.
- Sharing a Single Commit Between Branches:
Reusing certain features or fixes across different branches without merging all changes.
How to Use Cherry-Pick
Basic Steps for Cherry-Picking a PR
- On the project page, click on "Pull Requests," then select the PR you want to cherry-pick.
- Click the "Cherry-Pick" button on the PR details page.
- In the pop-up confirmation box, select the branch you want to cherry-pick this PR to, then click "Submit."
- Review and Merge:
If you check "Use a new Pull Request for Cherry-Pick," you will be directed to the new Cherry-Pick Pull Request page. After clicking "Create," the PR will enter the review process. Once approved, the selected commits will be applied to the target branch.
Basic Steps for Cherry-Picking a Single Commit
- On the project details page, click "History" to view the list of historical commits.
- Select the commit you want to cherry-pick.
- Click on "..." at the top right corner of the commit details page, then select "Cherry-Pick" to move this commit to the target branch.
Note: There are some limitations to Cherry-Pick:
- Conflicts May Arise: If the selected commit conflicts with other changes in the target branch, you need to manually resolve these conflicts.
- Commit History May Become Messy: Frequent use of Cherry-Pick may complicate the commit history, so it should be used with caution.
CherryPick allows developers to flexibly move commits between branches. When using it, pay attention to conflict resolution and dependencies to ensure code stability and consistency.