109

Working with git, after some 'commit', and a couple of 'push', I realized that am using the wrong branch !

Now I have to remove in some way my changes in wrong_branch and commit and push the changes in right_branch

What's the best (and simple) way to do that ?

thank you

Dogbert
  • 188,810
  • 39
  • 339
  • 353
Alessandro De Simone
  • 3,692
  • 6
  • 26
  • 37
  • possible duplicate of [Git: removing selected commits from repository](http://stackoverflow.com/questions/495345/git-removing-selected-commits-from-repository) – halfdan Jun 24 '11 at 09:17
  • Good question @tokland: 99% of ruby developers use git, and the project I am working is in RoR ... but I know this could not be a good motivation – Alessandro De Simone Jun 24 '11 at 09:25
  • 3
    From my point of view, this is not a duplicate of what halfdan remarked, as it is also about moving commits to another branch, not only removing them. – olenz Jun 24 '11 at 09:30

3 Answers3

159

switch to that branch, check the git log and git revert those commits individually. Once you have done that, switch back to the desired branch and there you can then use git cherry-pick to pick specific commits from the git refs and merge it into the right branch.

git checkout wrong_branch
git revert commitsha1
git revert commitsha2
git checkout right_branch
git cherry-pick commitsha1
git cherry-pick commitsha2

If the commits are grouped together and there are no commits pushed after your dirty commits, you can even use git reset to get that wrong branch to a state just before your commits and then follow that again using git cherry-pick to get your commits into the right branch.

git checkout wrong_branch
git reset commitsha3 #commit just before commitsha2
git checkout right_branch
git cherry-pick commitsha1
git cherry-pick commitsha2
Dhruva Sagar
  • 6,291
  • 1
  • 23
  • 31
  • Revert + cherry-pick seems a lot simpler than other possible methods involving resetting head and/or rebasing. – ChrisV May 20 '13 at 06:07
  • 2
    For git cherry-pick you can put multiple shas in a single line, i.e. `git cherry-pick commitsha1 commitsha2`. – ThomasW May 27 '16 at 02:53
  • Lifesaver! And not complicated. Thank you! – Craig Silver Jul 07 '17 at 04:48
  • what happens when you want to merge wrong_branch (e.g. develop) into right_branch (e.g. feature/X) then you get the reverts undoing the changes you cherry picked in. – timB33 Aug 07 '19 at 10:32
  • `revert` is so much cleaner and less dramatic than `reset` unless you really want to delete something sensitive (credentials?) – Qumber Aug 24 '20 at 11:32
4

The simplest way is using git rebase. Suppose that you have that setting:

A -- B -- C -- C1 -- C2 # right branch
          \
           \-- D -- C3 -- C4 # wrong branch

You want to move change C3,C4 to the right branch.

git checkout -b new_wrong_branch D
git checkout wrong_branch
git rebase D --onto right_branch
git checkout right_branch
git merge right_branch wrong_branch
git branch -d wrong_branch
git branch rename new_wrong_branch wrong_branch

Now the setting is

A -- B -- C -- C1 -- C2 -- C3 -- C4 # right_branch
          \
           \ -- D # wrong_branch

Then you have to push your results with force (IF nobody has synchronized with your remote repo yet):

git push -f remote:right_branch
Olivier Verdier
  • 41,410
  • 26
  • 94
  • 89
  • I've not tried this solution, I was already working out in the first answer (and it worked). I've not thought to at rebase, it looks like a good alternative, thank you – Alessandro De Simone Jun 28 '11 at 15:45
  • 1
    Rather than `git push -f`, better use `git push --force-with-lease`. At least, it ensures that remote ref will be updated only if nobody pushed other commits on top of your commits. – Pierre-Olivier Vares May 19 '16 at 09:12
2

A bit of shortcut adding to Dhruva's answer

git checkout wrong_branch
git revert commitsha1

git checkout right_branch
git push right_branch

git checkout wrong_branch
git reset commitsha2 #commit just before commitsha1
git push wrong_branch -f
tarikakyol
  • 495
  • 6
  • 13