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
git clone https://github.com/Cerchie/git-cherry-pick-tutorial
this repo.on line 5 in
cherry-pick.txt
, add "This sentence represents the last commit in the tutorial." andgit add .
andgit commit
.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 .
andgit commit
Make an addition, like "This sentence represents a middle commit", save, and
git add .
andgit commit
Make another addition, like "This sentence represents the latest commit", save, and
git add .
andgit 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)
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.