Cherry-picking in Git allows you to select specific commits from one branch and apply them to another branch. This can be particularly useful in maintaining multiple branches for different purposes, such as bug fixes, feature development, or experimenting with new ideas without disrupting the main codebase. In this guide, we'll explore how to cherry-pick a range of commits effectively, including selecting multiple commits, excluding specific commits, and applying commits to multiple branches.
Understanding git cherry-pick
Cherry-picking is a powerful Git feature that enables you to choose commit(s) from a branch and apply them onto your current branch. This operation is useful for porting bug fixes and features without merging entire branches, thus maintaining a clean project history.
Basic syntax of cherry-pick
The basic command for cherry-picking a single commit is:
git cherry-pick <commit-hash>
Where <commit-hash>
is the unique identifier for the commit you want to cherry-pick.
Cherry-picking a range of commits
To cherry-pick a sequence or range of commits, Git provides a straightforward way to specify commit ranges using the ..
(two dots) syntax.
Syntax for a range of commits
The command to cherry-pick a range of commits from commit A
to commit B
(inclusive) is:
git cherry-pick A^..B
Here, A^
means "the parent of A," ensuring that commit A
is included in the range. This syntax picks all commits starting just after commit A
's parent up to and including commit B
.
Example of cherry-picking multiple commits
Suppose you want to cherry-pick the last three commits from another branch called feature
. First, check out the branch where you want to apply the commits:
git checkout main
Then, find the hashes of the commits you want to cherry-pick (assuming they are the last three commits on feature
):
git log feature --oneline -3
This will list the last three commits. If the commits have hashes 1234567
, 2345678
, and 3456789
, you cherry-pick them as follows:
git cherry-pick 1234567^..3456789
Cherry-picking to multiple branches
To cherry-pick commits to multiple branches, you need to repeat the cherry-pick process for each branch. For example:
Cherry-pick commits to the first branch:
Terminalgit checkout branch1git cherry-pick A^..BSwitch to the second branch and repeat:
Terminalgit checkout branch2git cherry-pick A^..B
Excluding a commit while cherry-picking
To exclude a specific commit from a range, you must break the cherry-picking process into parts that skip the unwanted commit. For example, to cherry-pick from commit A
to D
but exclude commit C
, you can do:
git cherry-pick A^..Bgit cherry-pick D
Where B
is the commit just before C
, and D
is the commit just after C
.
Tips for successful cherry-picking
- Test each commit: After cherry-picking, especially with multiple commits, test your branch to ensure that the changes work as expected without introducing errors.
- Handle conflicts promptly: Cherry-picking can often lead to conflicts, particularly if the changes apply to parts of the code that have diverged. Resolve conflicts as they arise.
- Maintain clean history: If cherry-picking creates a messy history, consider using interactive rebase (
git rebase -i
) to tidy up before pushing to a shared repository.
For further reading on the git-cherry-pick command, see the official Git documentation.