Toby
Toby

Reputation: 10154

Git rebase child branch to master

I have the following situation in a Git repository

   A - B [origin/master]
    \
     C [origin/X]
      \
       E - F [origin/Y]

I didn't realise when I started Y, that I had branched from X, I had intended to branch from master.

How can I rebase Y onto master without including commit C?

(Changes on X are to a file not touched by the commits on Y)

I'd like to end up with the following:

   A - B [origin/master]
    \   \
     \   E - F [origin/Y]
      \
       C [origin/X]  

I tried git rebase master and it doesn't seem to have changed anything, output was :

Current branch Y is up to date.

Upvotes: 0

Views: 67

Answers (2)

simon-pearson
simon-pearson

Reputation: 1970

As is always the case in Git there are probably lots of ways of achieving this, however the following should work:

  1. On branch Y reset this to master: git reset --hard master.
  2. Cherry-pick your commits across: git cherry-pick C..F (assuming E and F are the first and latest commit IDs on your branch after C).

Edited: As @eftshift0 pointed out I initially had the wrong commit ID's in my git cherry-pick statement - fixed now.

Upvotes: 0

eftshift0
eftshift0

Reputation: 30307

It can be easily done like this:

git rebase --onto origin/master origin/X origin/Y

Given that you are using remote references, you will be on detached HEAD so you have to do this to push your resulting branch into origin's branch Y:

git push origin -f HEAD:Y

Upvotes: 2

Related Questions