- Cherry-pick a single commit
- Cherry-pick multiple commits
- Cherry-pick a merge commit
- Related topics
- Troubleshooting
Cherry-pick changes with Git
Use git cherry-pick
to apply the changes from a specific commit to your current
working branch. Use this command to:
- Backport bug fixes from the default branch to previous release branches.
- Copy changes from a fork to the upstream repository.
- Apply specific changes without merging entire branches.
You can also use the GitLab UI to cherry-pick. For more information, see Cherry-pick changes.
git cherry-pick
carefully because it can create duplicate commits and potentially
complicate your project history.Cherry-pick a single commit
To cherry-pick a single commit from another branch into your current working branch:
-
Check out the branch you want to cherry-pick into:
git checkout your_branch
-
Identify the Secure Hash Algorithm (SHA) of the commit you want to cherry-pick. To find this, check the commit history or use the
git log
command. For example:$ git log commit 0000011111222223333344444555556666677777 Merge: 88888999999 aaaaabbbbbb Author: user@example.com Date: Tue Aug 31 21:19:41 2021 +0000
-
Use the
git cherry-pick
command. Replace<commit_sha>
with the SHA of the commit you identified:git cherry-pick <commit_sha>
Git applies the changes from the specified commit to your current working branch. If there are conflicts, a notification is displayed. You can then resolve the conflicts and continue the cherry-pick process.
Cherry-pick multiple commits
To cherry-pick multiple commits from another branch into your current working branch:
-
Check out the branch you want to cherry-pick into:
git checkout your_branch
-
Identify the Secure Hash Algorithm (SHA) of the commit you want to cherry-pick. To find this, check the commit history or use the
git log
command. For example:$ git log commit 0000011111222223333344444555556666677777 Merge: 88888999999 aaaaabbbbbb Author: user@example.com Date: Tue Aug 31 21:19:41 2021 +0000
-
Use the
git cherry-pick
command for each commit, replacing<commit_sha>
with the SHA of the commit:git cherry-pick <commit_sha_1> git cherry-pick <commit_sha_2> ...
Alternatively, you can cherry-pick a range of commits using the ..
notation:
git cherry-pick <start_commit_sha>..<end_commit_sha>
This applies all the commits between <start_commit_sha>
and <end_commit_sha>
to your current working branch.
Cherry-pick a merge commit
Cherry-picking a merge commit applies the changes from the merge commit to your current working branch.
To cherry-pick a merge commit from another branch into your current working branch:
-
Check out the branch you want to cherry-pick into:
git checkout your_branch
-
Identify the Secure Hash Algorithm (SHA) of the commit you want to cherry-pick. To find this, check the commit history or use the
git log
command. For example:$ git log commit 0000011111222223333344444555556666677777 Merge: 88888999999 aaaaabbbbbb Author: user@example.com Date: Tue Aug 31 21:19:41 2021 +0000
-
Use the
git cherry-pick
command with the-m
option and the index of the parent commit you want to use as the mainline. Replace<commit_sha>
with the SHA of the merge commit and<parent_index>
with the index of the parent commit. The index starts from1
. For example:git cherry-pick -m 1 <merge-commit-hash>
This configures Git to use the first parent as the mainline. To use the second parent as the mainline, use -m 2
.
Related topics
Troubleshooting
If you encounter conflicts during cherry-picking:
- Resolve the conflicts manually in the affected files.
-
Stage the resolved files:
git add <resolved_file>
-
Continue the cherry-pick process:
git cherry-pick --continue
To abort the cherry-pick process and return to the previous state, use the following command:
git cherry-pick --abort
This undoes any changes made during the cherry-pick process.