# Using Git Cherry-Pick To Apply A Commit From Another Branch

> In this guide, we cover the cherry-pick command to pick a specific commit from another git branch and bring it into your current branch.

- Author: [CJ Saathoff](https://www.namehero.com/blog/author/cj-saathoff/)
- Published: 2024-05-01
- Updated: 2024-10-30
- Category: [Linux Command Line](https://www.namehero.com/blog/category/linux-command-line/)
- Web page: https://www.namehero.com/blog/using-git-cherry-pick-to-apply-a-commit-from-another-branch/

[![](https://blogcdn.namehero.com/blog/wp-content/uploads/2024/03/27104525/gitcherry-1024x576.jpg)](https://blogcdn.namehero.com/blog/wp-content/uploads/2024/03/27104525/gitcherry.jpg)

Git is a distributed version control system that is used to keep track of changes to a set of computer files. This useful tool has quite many sub-commands, in this post we will look into one of those sub-commands the cherry-pick command. The cherry-pick command allows you to pick a specific commit from another git branch and bring it into your current branch.

## Understanding Git Cherry-Pick

### Definition Of Cherry-Pick In Git

The cherry-pick sub-command in git is used to apply a single commit from one branch and apply it to the current branch in another.

### Purpose Of Cherry-picking A Commit

There are many reasons you may want to cherry-pick some examples are bug fixes, feature isolation, and backporting to name a few.

### Considerations

- Be aware cherry-picking only moves one commit to the new branch at a time.
- Be prepared to resolve merge conflicts when working with cherry picked commits.
- Document your cherry picks clearly, including their origin.
- Consider alternatives like rebasing or merging instead.

## Steps To Cherry-Pick A Commit From Another Branch

Make your current directory contain a git repository.

If you need a list of the current branches available for that repository use *git branch -r.*

### 1. Identify The Commit You Want To Cherry-pick

First, you will need the commit hash that you want to cherry-pick.

To look at existing commits to gather a commit hash, the git log is likely the easiest below is an example:

```

git log source-branch

```

- If you get an error due to the branch not being stored locally use *git checkout source-branch* and try again.
- Once you have git log information you will need to scroll through till you find the desired commit.
- Note the commit hash down. For reference, the commit hash is outlined in the example below.![Example of commit hash in git log output](https://images.surferseo.art/f86dabd9-2101-489c-9468-7d97f9413075.png)

### 2. Switch To The Target Branch

Now to change to the other branch, the branch that will be the target for the cherry pick. To change to the target branch you want to use the git checkout command, see the example below:

```

git checkout target-branch

```

- Make sure to replace target-branch with the branch name you want to apply the commit to.

### 3. Using The Cherry-pick Command

Now has come the time for the cherry-pick command.

```

git cherry-pick commit-hash --no-commit

```

- Replace commit-hash with the commit hash that we identified in step one.

### 4. Resolve Any Conflicts That Arise

If any conflicts come up you will be prompted to resolve them before continuing.

### 5. Commit The Changes

Once any conflicts are taken care of you will want to do a new commit using:

```

git commit

```

- You will then be asked to enter your commit message

### 6. Push The Changes

After you commit the changes make sure you remember to push them to the remote repository. Assuming the remote repository is already configured in this the command would be:

```

git push

```

## Conclusion

Git is unquestionably a powerful command-line tool. Git has many sub-commands that provide many functions. In this blog post, we discussed the cherry-pick sub-command its definition, purpose, as well as considerations. From there wrapped up to a step-by-step guide on how to use the command.

## Additional Links

Looking for more information what not check out these links:

- [Other posts on Git](https://www.namehero.com/blog/?s=git)
- [Git Offical Documentation](https://git-scm.com/doc)
- [Git cherry-pick Documentation](https://git-scm.com/docs/git-cherry-pick)

## Related posts

- [Git Checkout Remote Branch: How To Checkout a File From Another Branch](https://www.namehero.com/blog/git-checkout-remote-branch-how-to-checkout-a-file-from-another-branch/)
- [Git Rebase vs Merge vs Cherry-Pick: When to Use Each](https://www.namehero.com/blog/git-rebase-vs-merge-vs-cherry-pick-when-to-use-each/)
- [How to Reset the Last Commit in Git](https://www.namehero.com/blog/how-to-reset-the-last-commit-in-git/)
- [Git Branch Naming Conventions](https://www.namehero.com/blog/git-branch-naming-conventions/)
