Reset local repository branch to be just like remote repository HEAD

后端 未结 21 1477
挽巷
挽巷 2020-11-22 02:19

How do I reset my local branch to be just like the branch on the remote repository?

I did:

git reset --hard HEAD

But when I run a <

相关标签:
21条回答
  • 2020-11-22 03:01

    I did:

    git branch -D master
    git checkout master
    

    to totally reset branch


    note, you should checkout to another branch to be able to delete required branch

    0 讨论(0)
  • 2020-11-22 03:02

    I needed to do (the solution in the accepted answer):

    git fetch origin
    git reset --hard origin/master
    

    Followed by:

    git clean -f
    

    to remove local files

    To see what files will be removed (without actually removing them):

    git clean -n -f
    
    0 讨论(0)
  • 2020-11-22 03:02

    If you don't mind saving your local changes, yet still want to update your repository to match origin/HEAD, you can simply stash your local changes and then pull:

    git stash
    git pull
    
    0 讨论(0)
  • 2020-11-22 03:04

    If you want to go back to the HEAD state for both the working directory and the index, then you should git reset --hard HEAD, rather than to HEAD^. (This may have been a typo, just like the single versus double dash for --hard.)

    As for your specific question as to why those files appear in the status as modified, it looks like perhaps you did a soft reset instead of a hard reset. This will cause the files that were changed in the HEAD commit to appear as if they were staged, which is likely what you are seeing here.

    0 讨论(0)
  • 2020-11-22 03:07

    git reset --hard HEAD actually only resets to the last committed state. In this case HEAD refers to the HEAD of your branch.

    If you have several commits, this won't work..

    What you probably want to do, is reset to the head of origin or whatever you remote repository is called. I'd probably just do something like

    git reset --hard origin/HEAD
    

    Be careful though. Hard resets cannot easily be undone. It is better to do as Dan suggests, and branch off a copy of your changes before resetting.

    0 讨论(0)
  • 2020-11-22 03:08

    First, use git reset to reset to the previously fetched HEAD of the corresponding upstream branch:

    git reset --hard @{u}
    

    The advantage of specifying @{u} or its verbose form @{upstream} is that the name of the remote repo and branch don't have to be explicitly specified. On Windows or with PowerShell, specify "@{u}" (with double quotes).

    Next, as needed, use git clean to remove untracked files, optionally also with -x:

    git clean -df
    

    Finally, as needed, get the latest changes:

    git pull
    
    0 讨论(0)
提交回复
热议问题