简体   繁体   中英

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM