Git cherry-pick copies the changes from one or more source branch commits to a target branch. Unlike merge or rebase, cherry-pick lets you select specific source branch commits. For each source branch commit that you cherry-pick, Git creates a corresponding commit on the target branch.
You can cherry-pick to tackle these common tasks:
Deploy a specific feature from one branch to another.
Azure Repos provides limited support for cherry-picking, and only for the purpose of creating a pull request to apply a hotfix on a target branch. For more information, see Improving Azure DevOps cherry-picking.
The Cherry-pick option in the pull request menu in Azure Repos does the following:
Creates a new topic branch from the pull request's target branch.
Cherry-picks all changes from the pull request's source branch to the new topic branch.
Prompts you to create a new pull request to merge the new topic branch into another target branch.
The GitHub web interface doesn't support cherry-picking, but GitHub Desktop does. For step-by-step guidance on how to cherry-pick in GitHub Desktop, see Cherry-picking a commit.
Visual Studio 2022 provides a Git version control experience by using the Git menu, Git Changes, and through context menus in Solution Explorer. Visual Studio 2019 version 16.8 also offers the Team Explorer Git user interface. For more information, see the Visual Studio 2019 - Team Explorer tab.
Choose Git > Manage Branches to open the Git Repository window.
In the Git Repository window, right-click the target branch and choose Checkout.
In the Branches view, right-click the source branch and choose View History to open a commit History tab.
In the History tab, right-click the commit you want to cherry-pick and choose Cherry-Pick. Visual Studio doesn't support cherry-picking more than one commit at a time, so you'll need to repeat this step for each commit that you want to cherry-pick.
Visual Studio creates a new target branch commit that contains the changes from the cherry-picked commit. If the cherry-pick operation doesn't complete successfully, Visual Studio will notify you.
Visual Studio 2019 provides a Git version control experience by using the Git menu, Git Changes, and through context menus in Solution Explorer.
Choose Git > Manage Branches to open the Git Repository window.
In the Git Repository window, right-click the target branch and choose Checkout.
In the Branches view, right-click the source branch and choose View History to open a commit History tab.
In the History tab, right-click the commit you want to cherry-pick and choose Cherry-Pick. Visual Studio doesn't support cherry-picking more than one commit at a time, so you'll need to repeat this step for each commit that you want to cherry-pick.
Visual Studio creates a new target branch commit that contains the changes from the cherry-picked commit. If the cherry-pick operation doesn't complete successfully, Visual Studio will notify you.
Visual Studio 2019 version 16.8 and later versions provides a Git version control experience while maintaining the Team Explorer Git user interface. To use Team Explorer, uncheck Tools > Options > Preview Features > New Git user experience from the menu bar. You can use Git features from either interface interchangeably.
In Team Explorer, select the Home button and choose Branches.
In the Branches view, right-click the target branch and choose Checkout.
In the Branches view, right-click the source branch and choose View History to open a commit History tab.
In the History tab, right-click the commit you want to cherry-pick and choose Cherry-Pick. Visual Studio doesn't support cherry-picking more than one commit at a time, so you'll need to repeat this step for each commit that you want to cherry-pick.
Visual Studio creates a new target branch commit that contains the changes from the cherry-picked commit. If the cherry-pick operation doesn't complete successfully, Visual Studio will notify you.
Use the git log command to list source branch commits. The --oneline flag abbreviates the commit info.
git log --oneline <source branch>
Git lists the most recent commits first. Each commit ID is a partial SHA-1 hash that uniquely identifies the commit. For example:
e745d06 (HEAD -> add-network-controller) Add a test initialization class
a89f48e (origin/add-network-controller) Add fiber optic transceiver test
31da50b Add network switch test
e74baa2 (origin/main, origin/HEAD, test-fiber-optic-transmitter, main) Add readme content
0c14391 Add readme file
32e3946 Add project files.
Make a note of the ID of the commit that you want to cherry-pick.
Check out the target branch, if it isn't already checked out.
git checkout <target branch>
Commit, stash, or discard any uncommitted changes.
To cherry-pick a single commit:
git cherry-pick <commit ID>
To cherry-pick multiple commits, separate the commit IDs with spaces. The commits will be applied in the order that you enter them:
When you cherry-pick multiple commits, the default cherry-pick command creates a corresponding sequence of new target branch commits. To tell Git to stage and not commit the target branch changes, use the -n flag:
git cherry-pick -n <commit1 ID> <commit2 ID>
Then, you can manually create a single commit to contain all changes from the cherry-pick operation.
Git will notify you if there are merge conflicts during the cherry-pick operation. You can either resolve the conflicts and then run git cherry-pick --continue, or run git cherry-pick --abort to undo the cherry-pick operation.