Cherry-pick a commit
Cherry-picking a commit in Git allows you to selectively apply changes from one branch to another, enabling you to incorporate specific commits without merging the entire branch. This powerful technique is useful when you want to add a particular feature, bug fix, or improvement from one branch to another branch of your codebase.
In this part of our tutorial, we will explore the process of cherry-picking a commit in Git. By mastering cherry-picking, you'll have the flexibility to selectively incorporate valuable changes into different branches, fostering collaboration and accelerating project development.
Go to the git-tutorial/tutorial4
directory you previously downloaded.
When you examine the history of this repository, it will look like the following:
We are going to create a new commit in the main branch that is a copy of a commit on a different branch of the same repository. In this case, let’s copy the commit with the message “append description of the commit command.”
Switch over to the main branch. Use the git cherry-pick command and specify the commit hash for “append description of the commit command.”
The commit hash 99daed2
may differ depending on your local repository. You can obtain the commit hash by running the git log command in that branch. Find the commit hash of your local repository and use that instead.
$ git checkout main
Switched to branch 'main'
$ git cherry-pick 99daed2
error: could not apply 99daed2... commit
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'
As you can see above, a conflict has occurred. Manually resolve it and proceed committing the change.
$ git add sample.txt
$ git commit