简体   繁体   中英

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情况

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?

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM