Cherry-pick a Git commit (FREE ALL)
In Git, you can cherry-pick a commit (a set of changes) from an existing branch, and apply those changes to another branch. Cherry-picks can help you:
- Backport bug fixes from the default branch to previous release branches.
- Copy changes from a fork to the upstream repository.
You can cherry-pick commits from the command line. In the GitLab user interface, you can also:
- Cherry-pick all changes from a merge request.
- Cherry-pick a single commit.
- Cherry-pick from a fork to the upstream repository.
Cherry-pick from the command line
These instructions explain how to cherry-pick a commit from the default branch (main
)
into a different branch (stable
):
-
Check out the default branch, then check out a new
stable
branch based on it:git checkout main git checkout -b stable
-
Change back to the default branch:
git checkout main
-
Make your changes, then commit them:
git add changed_file.rb git commit -m 'Fix bugs in changed_file.rb'
-
Display the commit log:
$ git log commit 0000011111222223333344444555556666677777 Merge: 88888999999 aaaaabbbbbb Author: user@example.com Date: Tue Aug 31 21:19:41 2021 +0000
-
Identify the
commit
line, and copy the string of letters and numbers on that line. This information is the SHA (Secure Hash Algorithm) of the commit. The SHA is a unique identifier for this commit, and you need it in a future step. -
Now that you know the SHA, check out the
stable
branch again:git checkout stable
-
Cherry-pick the commit into the
stable
branch, and changeSHA
to your commit SHA:git cherry-pick <SHA>
Related topics
- Cherry-pick commits with the Commits API
- Git documentation for cherry-picks