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 <
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
.
The answer
git clean -d -f
was underrated (-d to remove directories). Thanks!
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