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 descr…


This content originally appeared on DEV Community and was authored by Lucia Cerchie

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

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


This content originally appeared on DEV Community and was authored by Lucia Cerchie


Print Share Comment Cite Upload Translate Updates
APA

Lucia Cerchie | Sciencx (2021-11-05T14:56:37+00:00) How to use git cherry-pick. Retrieved from https://www.scien.cx/2021/11/05/how-to-use-git-cherry-pick/

MLA
" » How to use git cherry-pick." Lucia Cerchie | Sciencx - Friday November 5, 2021, https://www.scien.cx/2021/11/05/how-to-use-git-cherry-pick/
HARVARD
Lucia Cerchie | Sciencx Friday November 5, 2021 » How to use git cherry-pick., viewed ,<https://www.scien.cx/2021/11/05/how-to-use-git-cherry-pick/>
VANCOUVER
Lucia Cerchie | Sciencx - » How to use git cherry-pick. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/05/how-to-use-git-cherry-pick/
CHICAGO
" » How to use git cherry-pick." Lucia Cerchie | Sciencx - Accessed . https://www.scien.cx/2021/11/05/how-to-use-git-cherry-pick/
IEEE
" » How to use git cherry-pick." Lucia Cerchie | Sciencx [Online]. Available: https://www.scien.cx/2021/11/05/how-to-use-git-cherry-pick/. [Accessed: ]
rf:citation
» How to use git cherry-pick | Lucia Cerchie | Sciencx | https://www.scien.cx/2021/11/05/how-to-use-git-cherry-pick/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.