2

I frequently end up with a series of branches built one from the other like this:

...-(M)
     |
     +-A
       |
       +-B
         |
         +-C

Usually with pull-requests for each of A, B and C.

The issue is that if I request a Pull request from C it includes all the changes made in the branches A and B, which just irritates people.

I would like to re-structure the graph such that it looks like:

...-(M)
     |
     +-A
     |
     +-B
     |
     +-C

But each time I attempt to rebase either 'B' or 'C' onto '(M)' the structure remains the same.

What would be the correct way to restructure this?

By the way, I use SourceTree for all my Git-related needs but am happy to translate from console instructions if necessary.

Edit: The solution suggested by VonC did indeed work however it would not work for me while I was using SourceTree's builtin version of Git. After downloading a portable version from here: https://github.com/git-for-windows/git/releases/tag/v2.14.2.windows.3 it worked fine.

Slartibartfast
  • 591
  • 5
  • 18

1 Answers1

2

Any rebase which does not involve all the previous commits is a rebase --onto command.

git checkout C
git rebase --onto M B C

That means: rebase everything after B (B excluded) up to C (C included) onto M.

git checkout B
git rebase --onto M A B

Note: for that, it is best to use the git bash of the latest Git for Windows (like PortableGit-2.14.2.3-64-bit.7z.exe unzipped anywhere you want). That will avoid weird bugs like "There is no tracking information for the current branch. Please specify which branch you want to rebase against." that I mentionned here.
A rebase is a local operation, and should not be concerned with remote branches.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thank you VonC. I have been trying very similar to that while following this post: https://makandracards.com/makandra/10173-git-how-to-rebase-your-feature-branch-from-one-branch-to-another However both the linked example (with 2 arguments) and your example (with three arguments) give the error message: "There is no tracking information for the current branch. Please specify which branch you want to rebase against." and I am struggling to understand what that means. – Slartibartfast Oct 16 '17 at 04:57
  • @Slartibartfast Strange, this should involve local branches: no remote tracking branch is needed. But if you must: https://stackoverflow.com/a/32729663/6309 – VonC Oct 16 '17 at 05:00
  • @Slartibartfast By the way, which Git version are you using, on which OS? And does git branch -v display A, B and C? – VonC Oct 16 '17 at 05:01
  • I'm using SourceTree's builtin version of Git (version 2.12.2.windows.2) on Windows 10. The 'master' branch I'm attempting to rebase onto is local but 'Upstream/master' is at the same point as well. – Slartibartfast Oct 16 '17 at 05:06
  • And yes, 'A', 'B' and 'C' are all displayed when I type "git branch -v" (as is 'M' - 'master'). Branch 'C' is coloured green, I assume to indicate that it's checkedout. I also used tab-completion to enter the branch names and each branch tab-completes properly. – Slartibartfast Oct 16 '17 at 05:12
  • Oh, something I just realised might be relevant is that while branches 'B' and 'C' have been 'committed' I have not yet 'pushed' them to 'origin' as I wanted to sought this issue out before pushing. – Slartibartfast Oct 16 '17 at 05:24
  • Don't matter,the rebase is purely a local operation. Don't rebase master. Rebase c, then b, as I mentioned in the answer. – VonC Oct 16 '17 at 05:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/156797/discussion-between-slartibartfast-and-vonc). – Slartibartfast Oct 16 '17 at 05:34