0

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?

AlBlue
  • 23,254
  • 14
  • 71
  • 91
Robson
  • 916
  • 5
  • 22

2 Answers2

2

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.

Community
  • 1
  • 1
David van rijn
  • 2,108
  • 9
  • 21
  • I don't want the whole commits, just some part of it, as I tried to explain I want that all the modifications done to 3 files go to the dev branch, and that they will not appear in the dev2 branch, if that is even possible. but it seems that I will have to go with the checkout>commit way – Robson Jul 03 '15 at 10:15
1

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
AlBlue
  • 23,254
  • 14
  • 71
  • 91
  • thanks for the answer, but yellow,brown,red are parts of a commit, not single commits, so cherry pick will get all changes from those commits and not just the things I need – Robson Jul 03 '15 at 12:41
  • 1
    In that case, you will have to do those changes manually. You can do a cherry-pick, reset one step back (i.e. HEAD^) and then do git checkout -- files-you-want-to-restore and use `git status` to figure out which parts you want to keep and which you don't - but you're going to end up with a different commit in any case so there's no difference with doing that and with doing it by hand. This is also why it makes sense to commit individual commits separately instead of all in one giant go, so you can cherry pick efficiently. – AlBlue Jul 03 '15 at 15:12
  • surely I didn't commit correctly, I surely should have not commited those files thogheter and should have created a separate commit – Robson Jul 03 '15 at 15:25