Apply only the changes from a new branch onto an older branch

72 Views Asked by At

I have 2 branches : feature1 and feature2 which are based on the develop branch. feature1 is based on an older commit while feature2 is more recent.

I want to get only the commits from feature2 that are not in develop onto feature1.

I tried git rebase --onto feature1 develop feature2 as suggested in this answer, but I see that commits from develop are still getting applied onto feature1. What am I missing here?

Update : It turns out that the local develop branch was not up-to-date with the remote develop while feature2 was based on a more recent commit on the remote develop. This is why the commits from the remote develop were getting rebased.

1

There are 1 best solutions below

11
On BEST ANSWER

As knittl mentions in the comments, git rebase --onto A B C always takes the commit range B..C (i.e. ^B C)

That means it should target commits from D (excluded) up to f2 HEAD (none of those commits should be from develop, reachable from develop):

          f2--f2 (feature2)
         /
--d--d--D--d (develop)
     \
      f1--f1 (feature1)

git rebase --onto feature1 develop feature2

--d--d--D--d (develop)
     \
      f1--f1 (feature1)
            \
             f2'--f2' (feature2)

feature1 was merged into feature2 earlier without deleting the feature1 branch.

So:

         f1--f1--f1--f1     (feature1)   
        /          \
       /        f2--f2--f2  (feature2)  
      /        /
--d--d--d--d--d             (develop)

However, the OP adds:

My local develop branch was not up-to-date with the remote develop while the other branches were up-to-date.
This is why the commits from the remote develop were getting rebased

So:

gi fetch
git switch feature1
git rebase --onto feature1 origin/develop feature2