— Git, Version Control, Cherry-Pick — 1 min read
Git is a powerful version control system that allows developers to manage and track changes in their projects. One useful feature of Git is git cherry-pick
, which enables you to select specific commits from one branch and apply them to another branch. This guide will walk you through the process of using git cherry-pick
effectively, along with examples to illustrate its usage.
Before getting started, please ensure that you have Git installed on your machine and have a basic understanding of Git concepts such as branches and commits.
git cherry-pick
To use git cherry-pick
, follow these steps:
1git checkout <target-branch>
git log
or other Git visualization tools.1git log
1git cherry-pick <commit-hash>
Git will attempt to apply the selected commit to the current branch. In case of conflicts, you will need to resolve them manually.
After resolving any conflicts, commit the changes.
1git commit
Congratulations! You have successfully cherry-picked a commit into your target branch. Repeat the above steps for each additional commit you want to apply.
Let's explore some examples to gain a better understanding of how git cherry-pick
works.
Suppose you have two branches, feature-branch
and main-branch
, where feature-branch
contains a commit with a bug fix that you want to apply to main-branch
.
main-branch
.1git checkout main-branch
feature-branch
.1git log feature-branch
main-branch
.1git cherry-pick <commit-hash>
1git commit
You have successfully cherry-picked the bug fix commit from feature-branch
to main-branch
.
In this scenario, let's say you need to apply multiple commits from one branch to another.
1git checkout target-branch
1git log source-branch
1git cherry-pick <commit-hash-1>2git cherry-pick <commit-hash-2>
1git commit
By following these steps, you can selectively cherry-pick multiple commits from one branch to another.