3

I've found some mistakes in my commit history. I used these steps to correct them and resolved all conflicts. Now my master branch is ok. But I've got another branch. Lets call it "Branch 1": enter image description here

I would like to apply the changes made to the master branch also to Branch 1. I've made the changes at the orange commit. I've resolved all conflicts until the last commit of the master branch. How can I apply these changes to Branch 1 also?

Community
  • 1
  • 1
Florian
  • 5,918
  • 3
  • 47
  • 86
  • There's probably a more elegant way, but if you really only have 3-4 unique commits on Branch 1 I'd consider creating a new branch off master and cherry-picking those 4 commits onto the new branch. – fzzfzzfzz May 30 '15 at 11:38
  • 1
    its simplified. there are more than 150 commits... and if i merge back branch1 i don't want any problems with that. i am no git prof. so i don't know whether it could cause any problems when merging back to master... – Florian May 30 '15 at 11:47
  • 2
    I think `rebase --onto` can help you. – Ôrel May 30 '15 at 11:58
  • could you give me a hint of how to use `--onto` correctly. I've found some material releated to the `--onto` switch but since i am quite new to git, i don't want to screw up. @Ôrel – Florian May 30 '15 at 13:43
  • You should have three branch. The new master, the old master, and the `git rebase --onto master old_master branch1` will take all commit between old_master and branch1 and put them on master. look `git rebse --help` you will find lot of details – Ôrel May 30 '15 at 14:09
  • I'm a bit puzzled by the diagram, as drawn, the changes in the orange commit *are* on branch 1 since branch 1 is a descendent of a commit that is descended from the orange commit. – Adam Parkin May 30 '15 at 20:30
  • thats exactly what i thought. but if i browse the changes from branch1 it is different from master. – Florian May 31 '15 at 12:20
  • I'm confused by your image. All changes of master are already part of "Branch 1". – knittl Oct 18 '22 at 10:00

2 Answers2

1

Making lots of assumptions here. Let's draw your current history anew, to make discussing it easier:

     .-D'-E'      -- master' (after rebase)
A-B-C-D-E         -- master  (before rebase)
         `F-G-H   -- branch1 (before rebase)

If I understood correctly, your final history should:

           .-F'-G'-H' -- branch1 (after rebase)
     .-D'-E'          -- master' (after rebase)
A-B-C-D-E             -- master  (before rebase)
         `F-G-H       -- branch1 (before rebase)

Or, hiding the old branches (before rebase):

A-B-C-D'-E'           -- master  (after rebase)
          `F'-G'-'H   -- branch1 (after rebase)

You should be able to achieve this by specifying which commit range to rebase and what your new upstream should be:

git rebase --onto "E'" E branch1

This will copy all commits E..branch1 and re-apply them on top of E'.

knittl
  • 246,190
  • 53
  • 318
  • 364
0

You can cherry pick the required commit. That would be the best to get the changes from other branch.

pramesh
  • 1,914
  • 1
  • 19
  • 30