Need to reset git branch to origin version

前端 未结 5 1863
时光说笑
时光说笑 2020-12-04 04:34

I was accidentally working on a branch I shouldn\'t have been for a while, so I branched off of it giving it the appropriate name. Now I want to overwrite the branch I shou

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-04 04:55

    Assuming this is what happened:

    # on branch master
    vi buggy.py                 # you edit file
    git add buggy.py            # stage file
    git commit -m "Fix the bug" # commit
    vi tests.py                 # edit another file but do not commit yet
    

    Then you realise you make changes on the wrong branch.

    git checkout -b mybranch    # you create the correct branch and switch to it
    

    But master still points to your commit. You want it to point where it pointed before.

    Solution

    The easiest way is:

    git branch --force master origin/master
    

    Another way is:

    git checkout master
    git reset --soft origin/master
    git checkout mybranch
    

    Note that using reset --hard will cause your uncommitted changes to be lost (tests.py in my example).

提交回复
热议问题