Middle
What is the Git Cherry-pick command and how is it used?
sobes.tech AI
Answer from AI
The Git Cherry-pick command is used to transfer a selected commit from one branch to another. It creates a new commit in the target branch containing the same changes as the original commit.
Usage:
- Switch to the branch where you want to copy the commit:
git checkout <target_branch_name>
- Apply
cherry-pickby specifying the commit hash:
git cherry-pick <commit_hash>
You can apply multiple commits if needed:
git cherry-pick <commit_hash_1> <commit_hash_2> ...
Or a range of commits:
git cherry-pick <start_commit_hash>..<end_commit_hash>
Features:
- Creates a new commit with a new hash, rather than simply copying the existing one.
- May lead to conflicts that need to be resolved.
- Useful for transferring bug fixes or individual features between branches without merging the entire branch.
- Does not alter the history of the original branch.
Alternatives:
git merge: combines the entire history of a branch.git rebase: rewrites commit history.
Example of transferring a bug fix from branch fix/bug-123 to develop:
git checkout develop
git cherry-pick f3a4c1b // Commit hash with the fix