Robson
Robson

Reputation: 926

How can I merge a file history from a branch to another in git?

So first check the following image, I tried to represent what I need git situation

The green squares are the commits, and the yellow, brown and red are parts of those commits (in my case commits to specifically 3 files spread through the commits).

There are no further changes made on dev

What actually happened: I was working on a branch and found out that some changes were actually useful on the dev branch and were not actually related to the dev2 branch. So I wanted that those changes on the dev branch.

git checkout
git merge

I actually want the modifications to get out of the dev2 branch and go into the dev branch, so if I do the checkout>commit the modifications will be on both branches, that's why I wouldn't like to do it.

Is this even possible? How can I do it?

Upvotes: 0

Views: 101

Answers (2)

AlBlue
AlBlue

Reputation: 24060

Yes, this is possible. You need to do three things:

  • Checkout the original branch: git checkout dev
  • Apply the commits that you want: git cherry-pick yellow, git cherry-pick brown, git cherry-pick red
  • Rebase dev2 onto dev: git checkout dev2, git rebase --onto dev

Upvotes: 1

David van rijn
David van rijn

Reputation: 2220

You can just find the sha of the last commit that should be in dev (the red square) by using git log, and then go to your master branch checkout master and just say git merge SHA where SHA of the red square. This is quite a common thing in git. It happens a lot with forked repos, to merge in the updates of the original.

Remember though that this gets the whole commits, i am not sure if that is what you want. If you want just some files from those commits, it will be a lot harder. Then you should have a look at this question.

Upvotes: 2

Related Questions