Sitemap
fab

fab

Hi, I am a Software Developer passionate about think the world.

How copy a commit from a branch to another in git?

2 min readMay 5, 2019

Sometimes we have a commit in a branch that we want to move to another branch. For example I have created a repository with 3 branches and in each one I added a file:

Press enter or click to view image in full size
Files in each branch

In master branch there is a file named hello ; in branch_1 there are 2 files hello and hola ; in branch_2 there are also 2 files hello and ola .

Get fab’s stories in your inbox

Join Medium for free to get updates from this writer.

Now we want to see the commits create in each branch.

Press enter or click to view image in full size
git log — oneline

Each branch have the master commit and other commit where we added changes. In this example we want to move the commit 7a48fca from branch_2 to branch_1 . In simple words we want to apply the changes did in branch_2 in branch_1 . To do it, we could use the command cherry-pick like git cherry-pick commit-id-to-move :

git cherry-pick 7a48fca

Now in branch_1 we have 3 commits:

git log in branch_1

And also we have 3 files:

So we finished. If you want to know more about this command you could see the documentation in https://git-scm.com/docs/git-cherry-pick.

--

--

fab
fab

Written by fab

Hi, I am a Software Developer passionate about think the world.