git,

Cherry picking commits in Git

Sep 11, 2022 · 1 min read · Post a comment

Cherry-picking in Git is all about choosing, picking and applying commit from any branch to the current HEAD branch. This is done using the git cherry-pick command. Let’s dive a bit into details.

Prerequisites

  • Git

Solution

Assuming we have the following branch tree:

c1 - c2 - c3 - c4  main
      \
      c5 - c6 - c7 feature

So, if we want to cherry pick c6 only to the main branch:

git checkout main
git cherry-pick c6

To find the commit ID run git log or visit How to list all commits in a Git repository for more details.

Now, the tree will look like:

c1 - c2 - c3 - c4 - c6  main
      \
      c5 - c6 - c7      feature

Note(s): git cherry-pick will create a new commit unless you pass the --no-commit option.

Conclusion

git cherry-pick command is used rarely, almost never, but I can see it being useful in a few scenarios including:

  • Restoring lost and removed commits.
  • Pushing commits in the wrong branch.
  • Hotfixing bugs.

To find more neat Git commands and hacks, browse the Git category. Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.

git