This is actually not stupid at all, I just wanted to reference David Letterman’s old “Stupid Pet Tricks” bit so I could have an excuse to post this hilarious image of a dog driving another dog in a little car:
ANYWAY. I’m going to talk about cherry-picking commits in Git, so you can merge only part of a branch into another branch without screwing everything up by merging everything at once and then melt your struggling brain trying to figure out where it all went wrong.
For an example of when this might be useful: this morning, I made a change to my ‘test’ branch that I also wanted in my ‘master’ branch, and had previously been copying and pasting from one branch into another in a very ungraceful way to avoid messing up certain precarious situations in one branch that would break the other. I knew there had to be a better way to pull those commits from ‘test’ into ‘master’ without breaking everything, and this is that way.
First, in your terminal, in the branch where the commit you want to merge lies (in this case, my ‘test’ branch), enter “git log.” It should look kinda like this:
I want to take that third commit down (28b4504) and merge that into my ‘master’ branch. The solution? CHERRY PICKING!
No, not that kind of cherry picking! DUH.
Git checkout to the branch you want to merge this commit into, and simply type:
git cherry-pick 28b4504
(Except, of course, you’d replace ‘28b4504’ with the commit of your choice from your own git log)
SHAZAM! Now that commit will be merged into that branch. So easy! Don’t you feel accomplished? Stephen Colbert dances with joy for you and your newfound cherry pickin’ skillz:
(ALSO: Big ups to Greg Lamp for showing me that cherry picking existed in the first place. Thanks Greg!)