4

I have an accident when develop. I have two commits which need at branch A but I pushed to branch B. So now, I want to move those commit to branch A and then remove them from branch B. Please view image for detail:

enter image description here

lee
  • 7,955
  • 8
  • 44
  • 60

1 Answers1

6

First, go to branchA and cherry-pick the two commits you want to pick here.

$ git checkout branchA
$ git cherry-pick <commit1>       # commit1 = 0a18e0f   
$ git cherry-pick <commit2>       # commit2 = e604ce4

$ git push origin HEAD            # push to remote

Now remove the two commits from branchB by revert or rebase. Revert is preferable because it does not change git history.

Revert:

$ git checkout branchB
$ git revert <commit1>
$ git revert <commit2>
$ git push origin HEAD

Rebase:

$ git checkout branchB
$ git rebase -i efb2443         # go back to the commit before the two commmits you want to remove

Now comment out (adding `#` before the commit hash) the two commits you want to remove.

$ git push -f origin HEAD       # you need to force(-f) push as history is changed here by rebasing
Sajib Khan
  • 22,878
  • 9
  • 63
  • 73
  • do I need do anything else after cherry-pick (to push code to server)? – lee Jan 23 '17 at 03:34
  • 1
    @lee It might be better to revert those two commits on branch B rather than remove them using interactive rebase (`rebase -i`). If someone (or many people) has pulled the changes that you previously pushed to branch B, you'll be rewriting history for them when you (force) push your updated branch. This will give them a headache, and is against git etiquette. – mattliu Jan 23 '17 at 03:45
  • Thanks so much for your help. – lee Jan 23 '17 at 04:11