GIT Cherry Pick


Git cherry-pick is a command used to apply a specific commit from one branch to another branch. This can be useful when you want to selectively apply changes from one branch to another without merging the entire branch. To use git cherry-pick, follow the steps below:

  1. Check out the branch to which you want to apply the commit(s) by running:
git checkout target-branch
  1. Identify the commit(s) that you want to apply. You can use git log to view the commit history and find the commit hash that you want to cherry-pick. For example:
$ git log --oneline
c4d83f4 added feature B
8e3e3af added feature A
d5b6b5c initial commit
  1. Use the git cherry-pick command followed by the commit hash(es) to apply the changes to the target branch. For example:
git cherry-pick 8e3e3af

This will apply the changes from the commit with hash 8e3e3af to the target branch.

  1. If there are conflicts, resolve them using the same process as resolving merge conflicts (as described in my previous answer). Once the conflicts are resolved, add the file(s) to the staging area and continue the cherry-pick process using the command:
git cherry-pick --continue
  1. If you want to apply multiple commits, you can specify a range of commits using the .. operator. For example:
git cherry-pick 8e3e3af..c4d83f4

This will apply the changes from all commits between 8e3e3af and c4d83f4 (inclusive) to the target branch.

Example:

Suppose we have two branches feature and main, and we want to apply the changes made in the feature branch commit with hash 8e3e3af to the main branch using git cherry-pick. We can do this by running:

$ git checkout main
$ git cherry-pick 8e3e3af

If there are no conflicts, the changes made in the feature branch commit with hash 8e3e3af will be applied to the main branch. If there are conflicts, we resolve them using the process described earlier and continue the cherry-pick process using the command:

$ git cherry-pick --continue

If we want to apply multiple commits, we can specify a range of commits using the .. operator. For example:

$ git cherry-pick 8e3e3af..c4d83f4

This will apply the changes from all commits between 8e3e3af and c4d83f4 (inclusive) to the main branch