Getting started with git cherry-pick

Photo by Yancy Min on Unsplash

Getting started with git cherry-pick

Table of contents

No heading

No headings in the article.

Git cherry-pick is a very useful command that allows you to retrieve a specific commit from one branch and apply it to another branch. This can be particularly useful if you want to retrieve a particular modification made on one branch, without having to merge the entire branch.

here is an illustration:

First, make sure that you are on the target branch where you want to apply the modifications.

Next, retrieve the hash of the commit you want to apply to the target branch. You can do this by using the git log command to display the commit history:

git log

Note the hash of the commit you want to apply. Use the git cherry-pick command followed by the hash of the commit you want to apply:

git cherry-pick <commit_hash>

This command will apply the modifications of the specified commit to the target branch. If conflicts arise during the application of the modifications, you will need to resolve them manually by editing the conflicting files. Once you have resolved the conflicts, use the git add command to add the modified files to the index, then use the git cherry-pick --continue command to complete the operation.

git add <file_name>
git cherry-pick --continue

If you do not want to continue with the cherry-pick, you can use the git cherry-pick --abort command to cancel the operation.

git cherry-pick --abort

If you are looking to retrieve multiple commits at once, you can specify a range of commits using the notation "commit1..commit2". For example, the following command will retrieve all commits between commits A and B inclusive:

git cherry-pick A..B

In summary, git cherry-pick is a very useful command for retrieving specific modifications from one branch and applying them to another branch. However, it is important to understand how this will affect the commit history of your project and to take the time to resolve any potential conflicts that may arise.

For more details, don't hesitate to consult the documentation :

https://git-scm.com/docs/git-cherry-pick