What is git cherry-pick and how to use it effectively
Tags: git, tutorial, beginners, programming
Published: 2020-05-21 12:59:53

---
title: What is git cherry-pick and how to use it effectively
published: true
tags: git, tutorial, beginners, programming
canonical_url: https://gitbetter.substack.com/p/how-to-use-git-cherry-pick-effectively
---

[![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3824e08-ded9-4b23-8faf-cc85b4a0398e_512x512.gif)](https://cdn.substack.com/image/fetch/c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3824e08-ded9-4b23-8faf-cc85b4a0398e_512x512.gif)

### This post was originally posted in the newsletter [GitBetter](https://gitbetter.substack.com/). If you are interested in leveling up your game in Git, you can subscribe to it.

git cherry-pick is one of the useful commands in Git that helps you to copy, paste commit(s) from one branch to another. Simple.

Note that cherry-pick will copy the commits so that the original commit will still be present in the source branch.

Most devs will consider cherry-picking is a bad practice that can cause problems like having duplicate commits in multiple branches, messing up with the git history, and others. But cherry-pick is one of the most powerful tools and if you understand how it works and if used with caution, then it can be really useful.

Some use cases of cherry-picking are that you may have some commits in the wrong branch and you can cherry-pick them to the right branch. Other use cases like you won’t need the entire branch to be merged (because it may have staled or something) but you need only particular commits. Then you can cherry-pick those commits to your branch.

### How to cherry-pick

```
git cherry-pick <commit hash>
```

As you know, you can get the commit hash from `git log` or others.

The above command will copy the commit with the corresponding hash and paste it in the current branch you are in.

[![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1bc753d9-e7a1-48b6-ae9f-ee0cc4be2f40_643x116.png)](https://cdn.substack.com/image/fetch/c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1bc753d9-e7a1-48b6-ae9f-ee0cc4be2f40_643x116.png)

In the above image, you can see that I have cherry-picked a commit and it has been applied to the current branch **`develop`**.

Consider the below commit history.

```
A - B - C - D - E - F (Branch A)
     \         
      G - H - I (Branch B)
```

If we gonna cherry-pick **I** from branch B then the commit history changes like below

```
 A - B - C - D - E - F - I
     \         
      G - H - I
```

You can see **I** is present in both branches now.

In some cases, you don’t need to commit the changes yet. You just need to copy all the changes.

```
git cherry-pick -n <commit hash> 
```

In this case, git will copy the commit and then it will stage all the changes but it won’t commit the changes in the current branch. You can commit the changes yourselves or you can un-stage some part of it or do anything else.

[![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F7a670e5d-7655-4114-be5d-3244c96d7415_682x199.png)](https://cdn.substack.com/image/fetch/c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F7a670e5d-7655-4114-be5d-3244c96d7415_682x199.png)

In the above image, you can see the cherry-picked changes are staged but not committed yet.

### git cherry-pick with conflicts

What if there were conflicts when you cherry-pick. Git will pause the cherry-pick. You have to resolve the conflicts and ask the git to continue with the cherry-pick.

[![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F82fcf708-f4ec-499c-b3f2-cf4a2d2338a3_632x328.png)](https://cdn.substack.com/image/fetch/c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F82fcf708-f4ec-499c-b3f2-cf4a2d2338a3_632x328.png)

In the above example, I have tried to cherry-pick a particular commit but it has resulted in a conflict. You can see the conflict in the above image. So we have to resolve the conflicts and then continue with the cherry-pick.

After resolving the conflicts, you can continue with the cherry-pick with the below command.

```
git cherry-pick —continue
```

If you decided to abort the cherry-pick with conflicts, you can use

```
git cherry-pick —abort
```

[![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa098ec72-cf3a-48af-b4d4-4f2f9a3bc6a5_524x132.png)](https://cdn.substack.com/image/fetch/c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa098ec72-cf3a-48af-b4d4-4f2f9a3bc6a5_524x132.png)

In the above example, I have resolved the conflict, then have staged the file. After that, I’m continuing with the cherry-pick.

### git cherry-pick a series of commits

```
git cherry-pick A..B
```

In the above command, A and B are commit hashes.

The above command will include all commits from A to B excluding A. And remember **A should be older than B.**

```
git cherry-pick A^..B
```

This command will include all commits from A to B including A.

### git cherry-pick a merge commit

Cherry-picking a merge commit is somewhat tricky. When there is a merge there are two branches included. A parent-B branch is merged into the parent-A branch.

So when you are cherry-picking a merge commit you have to specify whether you need to cherry-pick parent-A branch or the parent-B branch.

```
A - B - C - E - F (branch A)
     \     /
      G - H - I (branch B)
```

In this above case, **E** is the merge commit. And if you run **`git cherry-pick E `** then git will throw a warning. You have to mention the parent branch number.

You can do

```
git cherry-pick -m 1 E
```

This will take all commits from **C - E**.

Or you can do

```
git cherry-pick -m 2 E
```

This will take commits from **G - H - E**.

But when cherry-picking the merge commit, always proceed with caution. There is a high possibility that you can mess up with history. So you have to decide in the given situation whether cherry-picking a merge commit is good or not.

### Other useful commands I found

```
git cherry-pick <branch-name>
```

This command will copy only the latest commit from the given branch and apply it to the current branch. Remember only the latest commit in the given branch.

```
git cherry-pick ..<branch-name>
```

This command will take all the commits from the given branch that are older than the current branch you are in and apply it to the current branch.

Assume you have branches A and B. If you are applying this command from branch A, then all the commits from B that are older than branch A will be applied to branch A.

### Points to remember

1. **git cherry-pick <commit-hash>** to cherry-pick a commit from another branch.

2. **git cherry-pick -n <commit-hash>** to cherry-pick the commit but it won’t commit the changes. It will only stage all the changes.

3. **git cherry-pick —continue** or **git cherry-pick —abort** when you face conflicts while cherry-picking.

4. **git cherry-pick -m** to mention the parent branch number when you are cherry-picking a merge commit.

5. **git cherry-pick A..B** to cherry-pick a series of commits.


If you have any queries, feedback, or anything you can just add a comment.


#### *If you have come this much, then I think you are much interested in Git. You can subscribe to [GitBetter](https://gitbetter.substack.com/) to get tricks, tips, and advanced topics of Git.*

####