Git & GitHub - Cherry-Picking Commits
How to cherry-pick commits in Git
Cherry-picking is a powerful feature in Git that allows you to apply specific commits from one branch into another. This guide covers how to use cherry-picking effectively to manage your codebase.
Key Points:
- Cherry-picking allows you to select specific commits to apply to another branch.
- It is useful for applying bug fixes or features from one branch to another without merging the entire branch.
- Cherry-picking can lead to conflicts, which need to be resolved manually.
Cherry-Picking Basics
To cherry-pick a commit, you need to know the commit hash (SHA) of the commit you want to apply. You can find the commit hash using git log
or by viewing the commit history on GitHub.
Cherry-Picking a Single Commit
Use the git cherry-pick
command followed by the commit hash:
# Example: Cherry-pick a single commit
$ git checkout target-branch
$ git cherry-pick COMMIT_HASH
Cherry-Picking Multiple Commits
You can cherry-pick multiple commits by specifying a range of commits or a list of individual commits:
# Example: Cherry-pick a range of commits
$ git cherry-pick START_COMMIT..END_COMMIT
# Example: Cherry-pick multiple individual commits
$ git cherry-pick COMMIT_HASH1 COMMIT_HASH2 COMMIT_HASH3
Resolving Conflicts
Conflicts can occur during cherry-picking if the changes in the commits conflict with the current state of the target branch. Git will pause the cherry-pick process and allow you to resolve the conflicts:
# Example: Resolving conflicts during cherry-pick
$ git cherry-pick COMMIT_HASH
# If there are conflicts, resolve them in the affected files
$ git add resolved-file
# Continue the cherry-pick process
$ git cherry-pick --continue
# If you want to abort the cherry-pick
$ git cherry-pick --abort
Using Cherry-Pick in Real-World Scenarios
Applying Hotfixes
Cherry-picking is often used to apply hotfixes from a main branch to a release branch:
# Example: Cherry-pick a hotfix from master to release branch
$ git checkout release-branch
$ git cherry-pick COMMIT_HASH
Backporting Features
Cherry-picking can also be used to backport features from a development branch to an older release branch:
# Example: Cherry-pick a feature from develop to an older release branch
$ git checkout old-release-branch
$ git cherry-pick COMMIT_HASH
Selective Merge
Sometimes, you may want to merge only specific changes from one branch to another without merging the entire branch:
# Example: Cherry-pick selected commits from a feature branch to master
$ git checkout master
$ git cherry-pick COMMIT_HASH1 COMMIT_HASH2
Best Practices
Follow these best practices to use cherry-picking effectively:
- Keep Commit Messages Clear: Write clear and descriptive commit messages to make it easier to identify the changes when cherry-picking.
- Avoid Overusing Cherry-Pick: Use cherry-pick judiciously to avoid creating a complex commit history.
- Resolve Conflicts Carefully: Pay attention to conflicts during cherry-pick and resolve them carefully to ensure code integrity.
Summary
This guide covered how to cherry-pick commits in Git, including the basics of cherry-picking, resolving conflicts, real-world use cases, and best practices. Cherry-picking is a powerful tool that allows you to selectively apply changes from one branch to another, helping you manage your codebase more effectively.