Signup/Sign In

How to Cherry Pick Git Commits?

Cherry-picking is the process of selecting a commit and adding that commit to some other branch. Unlike merging or rebasing that applies the changes of an entire branch to some other branch, Git Cherry-Pick just applies the changes of a single commit. It is mostly used when we have wrongly committed to some other branch or when a feature involved in one commit is also needed somewhere else.

Cherry Picking

Cherry-Picking

Cherry-picking is the process of selecting commits and applying them to other branches. The commits that are picked are not removed from their branch and just a copy of the content of that commit is made. Git then creates an entirely new commit that has all of the copied content and adds it to the destination branch.

Picking a commit and adding it to another branch by using cherry-picking

When to use Git Cherry-Pick?

  • Cherry-Pick is often used when we only want the changes from one commit to being added to some other branch. In these cases, a Merge does not make much sense as we will be left with a lot of unwanted commits on our branch.
  • Cherry-Pick is also used when we have accidentally committed to some other branch.
  • But Cherry-Pick should be used only when it is absolutely needed. This is because Git-Cherry pick creates a new commit on some other branch that has exactly the same content as some other commit. Using it multiple times will lead to a lot of duplicates and will make our history difficult to understand. Merge or Rebase are preferred over Cherry-Pick as they have no such problem.

Git cherry-pick command

Git Cherry-Pick is a pretty straightforward command. To use this command we must be checked out on the branch to which we want to add a commit. And we should know the hash of the commit that we are copying.

$ git cherry-pick <commit-hash>

We can also change the message of the copied commit before adding it to our branch by using the --edit option. This will open the configured text editor where you can alter the commit message.

$ git cherry-pick --edit <commit-hash>

We can also use the cherry-pick command to just copy the content of a commit to our working directory without adding a new commit to our branch. This is often used when we have to change the content of a commit before adding it to our branch. To do this we use the --no-commit option.

$ git cherry-pick --no-commit <commit-hash>

We can also cherry-pick multiple commits from some other branch to our current working branch. In the following example, all the commits starting from commit A to commit D will be copied to our current branch. A and D are the commit hash of the two commits and A is the older than D.

$ git cherry-pick A^..D

Git Cherry-Pick can also lead to merge conflicts. In these cases we can use the --continue option after resolving the conflict to continue the cherry-picking process or we can use the --abort option to abort the process.

Summary

Git Cherry-Pick is a very useful command that lets us add commits from other branches to our current branch. But it should be used in moderation as it creates copies of commits and makes it difficult to understand the project history. However, there are cases like creating a Hotfix or copying a commit from an unused branch where Cherry-Picking proves to be very helpful.