lab 31 Cherry-Picking
Goals
- Learn how to get work from another branch by cherry-picking the commit into your branch.
Switch to a New Branch and Create a New Change
Let’s make a new branch with a new change.
Execute:
git checkout -b excitement
lib/greeter.rb
class Greeter def initialize(who, excited = true) @who = who @excited = excited end def greet "Hello, #{@who}#{punctuation}" end def punctuation @excited ? "!" : "." end end
Execute:
git add lib/greeter.rb git commit -m "Add excitement"
Switch Back to the Greeter Branch and Cherry-Pick the Change
The cherry-pick operation allows you to copy commits directly onto your branch. The commits can come from anywhere as long as git knows about their hash identifier. This is useful when you don’t necessarily want all of the work from a colleague’s branch and just want part of it.
Execute the following commands using the hash for the commit you just created. Before doing the cherry-pick you’ll confirm that this is indeed the correct commit by viewing it.
Execute:
git checkout greet git show <hash> git cherry-pick <hash>
Output:
$ git show 71425e9 commit 71425e9d4a402f475bbc37dbfc985a6574fb0a4a Author: Jim Weirich <jim (at) edgecase.com> Date: Mon Oct 24 19:02:02 2022 +0000 Add excitement diff --git a/lib/greeter.rb b/lib/greeter.rb index 69681ef..95d9419 100644 --- a/lib/greeter.rb +++ b/lib/greeter.rb @@ -1,8 +1,12 @@ class Greeter - def initialize(who) + def initialize(who, excited = true) @who = who + @excited = excited end def greet - "Hello, #{@who}" + "Hello, #{@who}#{punctuation}" + end + def punctuation + @excited ? "!" : "." end end $ git cherry-pick 71425e9 [greet 71425e9] Add excitement Date: Mon Oct 24 19:02:02 2022 +0000 1 file changed, 6 insertions(+), 2 deletions(-)
Up Next
Now that you know how to cherry-pick you might wonder how to get work that isn’t already in your git repository. You’ll learn how to use patch-apply next.