1

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.

Abhilash
  • 85
  • 3
  • 11
  • That command should be fine. What does yoru commit history graph look like? – knittl Apr 04 '23 at 06:32
  • 1
    "Update : It turns out that the local develop branch was not up-to-date with the remote develop. This is the reason why the command was not working earlier. This is working now." – so "not reproducible or caused by a typo". If you wanted to rebase on the remote develop, you should use `origin/develop`. I'm voting to close, the command (`rebase --onto feature1 develop feature2`) works just fine (as indicated by OP). – knittl Apr 04 '23 at 07:06

1 Answers1

1

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
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • OP's command would work with this commit graph just as fine, no need for `merge-base`. `git rebase --onto feature1 develop feature2` would only take rebase commits `f2` and `f3` – knittl Apr 04 '23 at 06:31
  • @knittl Intuitively, I thought it would have worked only if feature2 was based on develop HEAD, not on develop past commit. – VonC Apr 04 '23 at 06:33
  • `git rebase --onto A B C` always takes the commit range `B..C` (i.e. `^B C`). Which other range would make sense? – knittl Apr 04 '23 at 06:34
  • @knittl So the graph history must not be the one I described. – VonC Apr 04 '23 at 06:35
  • Somehow this does not work, I have the same issue as before – Abhilash Apr 04 '23 at 06:37
  • @Abhilash can you show the result of `git log --decorate --oneline --graph --all --branches`? – VonC Apr 04 '23 at 06:39
  • @VonC I am unable to share this info here, I had believed that the situation is as you described, but I guess this is not true. Could you please suggest what I can look for? – Abhilash Apr 04 '23 at 06:45
  • @Abhilash Check the nature of the commit you think are from `develop` after rebase: as knittl mentioned, none of the rebased commits should be from `develop`. – VonC Apr 04 '23 at 06:49
  • @Abhilash After seeing your edit, I tried a new Git history schema: would it represent your current situation? – VonC Apr 04 '23 at 07:00
  • @VonC Your new schema is accurate, but I found out the cause of the issue. 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. – Abhilash Apr 04 '23 at 07:07
  • 1
    @Abhilash Good catch: I have included your comment in the answer for more visibility. – VonC Apr 04 '23 at 07:09