DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 967,611 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for How to use git cherry-pick
Lucia Cerchie
Lucia Cerchie

Posted on

How to use git cherry-pick

This is a blog post tutorial explaining how to use git cherry-pick. It has a bit more info on git cherry-pick than the README in my project, but you can jump ahead and just use the github tutorial.

First, what is git cherry-pick ?

The description from the documentation says:

"Given one or more existing commits, apply the change each one introduces, recording a new commit for each."

Well, what does that mean? I think of it as basically re-writing over your old commits, but learning-by-doing is best!

Let's get started:

Tutorial

  1. git clone https://github.com/Cerchie/git-cherry-pick-tutorial this repo.

  2. on line 5 in cherry-pick.txt, add "This sentence represents the last commit in the tutorial." and git add . and git commit.

  3. run git log to find the number for the 'wrong' commit:

commit 01ec7674c359004493950a17fbe0a42d6b08c2de
Author: Lucia Cerchie <lucia@stepzen.com>
Date:   Thu Jul 29 13:29:03 2021 -0700

    cherry-pick me
Enter fullscreen mode Exit fullscreen mode

Run git cherry-pick 01ec7674c359004493950a17fbe0a42d6b08c2de -x

You may need to resolve some conflicts.

Optional:

Learn how to cherry-pick multiple commits!

  • Remove your addition to line 5, save, and git add . and git commit

  • Make an addition, like "This sentence represents a middle commit", save, and git add . and git commit

  • Make another addition, like "This sentence represents the latest commit", save, and git add . and git commit

  • run git log to find the number for the first commit, and the latest commit.

Now, if you want to include the first commit you made (the removal of your addition to line 5) run:

git cherry-pick commit_number_first_commit^..commit_number_of_last_commit

If you don't want to include it, run:

git cherry-pick commit_number_first_commit..commit_number_of_last_commit, without the carat.

source for optional portion: https://stackoverflow.com/questions/1670970/how-to-cherry-pick-multiple-commits


Conclusion

I hope this tutorial was helpful! If you ran into trouble or if I can provide more accurate or clear information, let me know in the comments!

Top comments (1)

Collapse
 
hunktvapk profile image
hunktvapk

Actually,Nice post and the usage of git cherry pick is which I was searching and I found it here.Thank you for the post. From best regard Hunk TV Apk Tq.

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.