Skip to main content
  1. Learn
  2. Software Development
  3. Guides
  4. Git tutorial
  5. How to use Git
  6. How to manage history in Git
  7. Cherry-pick a commit
GuidesSoftware DevelopmentBacklog
Git

Project and code management together.

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:

Current history

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

Subscribe to our newsletter

Learn with Nulab to bring your best ideas to life