Codementor Events

How to merge a specific commit into another branch

Published Feb 15, 2018

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

Discover and read more posts from Richard Umoffia
get started
post comments1Reply
Bo Snyder
5 years ago

I need to get details about the merging the specific commit into another branch that was much easy. I read enough data on this finally I got useful answer from https://topamericanwriters.com/superiorpapers-com-review/ this was belong to the that have some worth at international communities.