Forgot to create new branch. How to transfer changes to new branch

前端 未结 6 2039
闹比i
闹比i 2021-02-19 13:45

this happens to every developer time-to-time. You start coding a new feature and forget to branch first locally.

So if that happens is there a way I can say hey, transf

6条回答
  •  [愿得一人]
    2021-02-19 13:56

    Unless you have done any changes that should not enter the feature branch after you have started work on the feature, it's as simple as creating the feature branch and rewinding the erroneously advanced master branch to the last commit that should not be part of the feature branch:

    MA --- MB --- MC --- FA --- FB --- FC <- master
    
    git checkout -b feature
    
    MA --- MB --- MC --- FA --- FB --- FC <- feature
                                        ^
                                        |
                                      master
    
    git branch -f master MC
    
    MA --- MB --- MC --- FA --- FB --- FC <- feature
                   ^
                   |
                 master
    

    If you have actually mixed your commits, you have a much larger problem. In that case, you need to perform a rebase to unmix the commits, so that you can proceed with the steps above:

    MA --- FA --- MB --- MC --- FB --- FC <- master
    
    git rebase -i MA
    #reorder the lines by moving the commit FA down after MC
    
    MA --- MB' --- MC' --- FA' --- FB' --- FC' <- master
    
    #proceed as above
    

    Be aware that you are rewriting history with this: If you have already published FA, you are in trouble. In that case, others will notice that you screwed up one way or the other, and the correct solution will include significant amounts of communication between humans.

提交回复
热议问题