DEV Community

Richard Umoffia
Richard Umoffia

Posted on

How to merge a specific commit into another branch

I really hope this title is self-explanatory enough cause this was the best I could think of. I know we are all very busy so I'll go straight to the point.

So for the last week, I've been working on this new feature for a project. I worked dev branch, as usual, then yesterday when we discovered a niggling bug that needed fixing immediately.

After fixing the error, I realized I had done this on the same branch that I've been working on for the last week. I couldn't push this branch to master cause everything will break.

I googled a bit and found something that worked but didn't solve my problem. Here's what I found:

First you checkout the branch you want to merge the commits into

git checkout <the branch>.

Then

git cherry-pick <the branch you've been working on>.

What this does is, "It applies the change introduced by the commit at the tip of the working branch and creates a new commit with this change". The issue I had with that was, I had made a couple of commits and I needed all those commits in the master branch. That's when I found it.

What did I find? you may be asking. Gitlens is what I found. If you use VScode you might know this extension. I've had it installed for some time now but I've never gotten around actually using it.

With this extension, all you need to do is, click the branches dropdown in the gitlens area, then proceed to click the drop-down for the branch you made the commits and then right click on a commit and cherry-pick it. Don't worry about all that gibberish I typed there. I've included a screenshot. (I'm nice, I know).

gitlens screenshot

When you look at your terminal, what you'll see is this command being run

git cherry-pick -e <the commit hash>

After running that command, you can then push your changes. You can download the gitlens extension to make use of this feature, or if you don't VScode yet, then there's a terminal command you. There's a command for everyone.

I promised to keep this short, so goodbye! I hope this helps.

bye!!

Top comments (11)

Collapse
 
elarcis profile image
Elarcis • Edited

The command line alternative would have been git cherry-pick <commitA..commitB>, where commitA is the commit right before the oldest commit to pick, and commitB the last one. You can check commits hashes either via GitLens or git log.

Collapse
 
chrisvasqm profile image
Christian Vasquez

You can have my 🦄 because you showed it both in the Terminal and VS Code 😎

Collapse
 
sadiasher profile image
Sadia

awesome article, it saved my time

Collapse
 
khai-jt profile image
Khai J. Thani

This helped me. Thanks!

Collapse
 
kalecio profile image
Kalécio

Thanks for saving me today <3

Collapse
 
prosenjitmanna profile image
Prosenjit Manna

Thank you

Collapse
 
ludakhris profile image
Mind Yo Chow

Great post I love "I promised to keep this short, so goodbye! I hope this helps."

Collapse
 
rifqiahmaddani profile image
rifqiahmaddani

Thank you, straight to the point!

Collapse
 
alais29dev profile image
Alfonsina Lizardo

you just saved me from wasting hours of my life, thank you!

Collapse
 
fabiancnieto_34 profile image
Fabian Nieto

Nice post and direct to the point, thank you!