Cherry-pick a Git commit

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:

You can cherry-pick commits from the command line. In the GitLab user interface, you can also:

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):

  1. Check out the default branch, then check out a new stable branch based on it:

    git checkout main
    git checkout -b stable
  2. Change back to the default branch:

    git checkout main
  3. Make your changes, then commit them:

    git add changed_file.rb
    git commit -m 'Fix bugs in changed_file.rb'
  4. Display the commit log:

    $ git log
    commit 0000011111222223333344444555556666677777
    Merge: 88888999999 aaaaabbbbbb
    Date:   Tue Aug 31 21:19:41 2021 +0000
  5. 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.

  6. Now that you know the SHA, check out the stable branch again:

    git checkout stable
  7. Cherry-pick the commit into the stable branch, and change SHA to your commit SHA:

    git cherry-pick <SHA>