Junior
What is cherry-pick in the context of version control systems?
sobes.tech AI
Answer from AI
Cherry-pick in Git is an operation that allows applying changes from a specific commit to another branch. It is useful when you need to transfer certain fixes or features from one branch without transferring all subsequent commits.
Usage:
- Selecting a commit: Determine the commit hash that needs to be "copied".
- Switching to the target branch: Switch to the branch where you want to apply the changes.
- Executing cherry-pick: Run the command
git cherry-pick <commit-hash>. Git will attempt to apply the changes from the selected commit to the current branch.
# Switch to the target branch
git checkout main
# Apply changes from the commit with hash abcdef1
git cherry-pick abcdef1
Possible use cases:
- Fixing an error in an old release branch.
- Transferring a specific feature from development to staging or production.
- Reverting changes from a specific commit (using the
-noption and subsequentgit revert).
Differences from merge and rebase:
mergecombines all changes from one branch into another.rebasemoves all commits of one branch on top of another.cherry-picktransfers only the selected commit, creating a new commit with the same changes in the target branch.
It is important to remember that cherry-pick creates a new commit with a new hash. This can lead to duplication of changes if the same commit is later merged into the target branch by another method. Use with caution and understand the consequences.