Git push the wrong branch

Working with git, after some "commit" and a few "push", I realized that I was using the wrong branch!

Now I need to somehow remove my changes in the wrong_branch and commit and push the changes in right_branch

What is the best (and easiest) way to do this?

Thank you

+61
git github
Jun 24. 2018-11-11T00:
source share
3 answers

switch to this branch, check that the git log and git revert tags do individually. Once you do this, go back to the desired branch, and then you can use git cherry-pick to select specific commits from the git links and merge them into the desired 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 after your dirty commits, you can even use git reset to get this wrong branch to the state immediately before your commits, and then repeat this with git cherry-pick to get your commit in the correct branch.

 git checkout wrong_branch git reset commitsha3 #commit just before commitsha2 git checkout right_branch git cherry-pick commitsha1 git cherry-pick commitsha2 
+80
Jun 24 2018-11-11T00:
source share

The easiest way is to use git rebase . Suppose you have this parameter:

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

You want to push changes 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 

Setting now

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

Then you need to force push your results (if no one has yet synced with your remote repo):

 git push -f remote:right_branch 
+2
Jun 24 '11 at 9:45 a.m.
source share

Slightly adding a shortcut 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 
+1
Aug 03 '16 at 16:10
source share



All Articles