15

I have 2 branches a master and an experimental. A shown:

master-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-Y
                  \
                   -x-x-x-x

My experimental is quite outdated and i was hoping to update it by simply copying the last commit in the master branch (Y) to experimental:

master-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-Y
                  \
                   -x-x-x-x-Y

If possible, I don't want to have to do any merging I want to overwrite anything in the experimental (master is my main priority).

Edit: Let me briefly explain the situation: When i try to merge commits at the tips of the master and experimental branch, I get a lot of merge conflicts! The same happens if i try to cherry-pick from the experimental onto the master! I was hoping to avoid them as i simply don't want any of the changes on the experimental! Up until now, I have been cherry-picking from master to experimental and when there are merge conflicts, I just keep changes of master branch. But after doing it many times, i was hoping that there may be some way in which i can do something like a merge except where (i am not prompted with any merge conflicts as master changes is all i need (for all I know it wouldn't matter what was previously on the experimental branch!

reubenjohn
  • 1,351
  • 1
  • 18
  • 43

2 Answers2

23

To cherry-pick just commit Y from master onto experimental:

git checkout experimental
git cherry-pick Y

Alternatively:

git checkout experimental
git cherry-pick master

...Will apply the change introduced by the commit at the tip of the master branch and create a new commit in experimental with this change.

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • 1
    I am sorry but i have to merge if i follow your instructions. I was hoping to be able to do so without a merge! – reubenjohn Sep 12 '13 at 19:25
  • @reubenjohn: Then I'm afraid I don't understand what you mean by "simply copying the last commit in the master branch (Y) to experimental". – johnsyweb Sep 12 '13 at 19:28
  • 1
    @reubenjohn take a short time h to learn more about cherry pick and merge, it's two different things – CharlesB Sep 12 '13 at 19:35
  • 1
    @CharlesB I do know the difference between cherry-pick and merge, but unfortunately I have mislead you with a bad question! There has been a misunderstanding for which i have edited my question! – reubenjohn Sep 13 '13 at 17:45
3

Here is what I initially tried!:

git checkout experimental
git rebase --onto <COMMIT ID OF LAST MASTER COMMIT> experimental

On trying this code, I found that not only the commit at the tip of the master but also the entire experimental branch became a clone of the master! Yes, it is not an elegant solution and a bad usage of git!

However, as pointed out by @Johnsyweb, the optimum solution would be to do a merge of experimental and master but,

with Git preferring to take the changes on master over the changes on experimental

Hence doing a:

git checkout experimental
git merge -Xtheirs master

should work fine. Although it says merge, since it only considers the changes of the experimental branch over the master one, it is more like a copy from experimental to master for all practical purposes.
(See a similar discussion here)

Community
  • 1
  • 1
reubenjohn
  • 1,351
  • 1
  • 18
  • 43
  • 1
    I'm not sure what you expected `git rebase` to do, but that looks right to me. It looks to me that you want to merge **all** of the changes on `master`, **since the branch-point**, into `experimental` but with Git preferring to take the changes on `master` over the changes on `experimental` where conflicts exist. Something like this: http://stackoverflow.com/a/3364506/78845 – johnsyweb Sep 13 '13 at 23:20