Reset local repository branch to be just like remote repository HEAD

后端 未结 21 1579
挽巷
挽巷 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:19

    Provided that the remote repository is origin, and that you're interested in branch_name:

    git fetch origin
    git reset --hard origin/<branch_name>
    

    Also, you go for reset the current branch of origin to HEAD.

    git fetch origin
    git reset --hard origin/HEAD
    

    How it works:

    git fetch origin downloads the latest from remote without trying to merge or rebase anything.

    Then the git reset resets the <branch_name> branch to what you just fetched. The --hard option changes all the files in your working tree to match the files in origin/branch_name.

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

    The answer

    git clean -d -f
    

    was underrated (-d to remove directories). Thanks!

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

    The only solution that works in all cases that I've seen is to delete and reclone. Maybe there's another way, but obviously this way leaves no chance of old state being left there, so I prefer it. Bash one-liner you can set as a macro if you often mess things up in git:

    REPO_PATH=$(pwd) && GIT_URL=$(git config --get remote.origin.url) && cd .. && rm -rf $REPO_PATH && git clone --recursive $GIT_URL $REPO_PATH && cd $REPO_PATH
    

    * assumes your .git files aren't corrupt

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