Git push to wrong branch

后端 未结 3 1115
悲&欢浪女
悲&欢浪女 2021-01-29 21:32

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_branc

3条回答
  •  攒了一身酷
    2021-01-29 22:04

    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
    

提交回复
热议问题