How do I reset 'master' to 'origin/master'?
As KindDragon's answer mentions, you can recreate master
directly at origin/master
with:
git checkout -B master origin/master
The git checkout
man page mentions:
If -B
is given, <new_branch>
is created if it doesn’t exist; otherwise, it is reset. This is the transactional equivalent of
$ git branch -f <branch> [<start point>]
$ git checkout <branch>
Since Git 2.23+ (August 2019), since git checkout
is too confusing, the new (still experimental) command is git switch
:
git switch -C master origin/master
That is:
-C <new-branch> --force-create <new-branch>
Similar to
--create
except that if<new-branch>
already exists, it will be reset to<start-point>
.
This is a convenient shortcut for:$ git branch -f <new-branch> $ git switch <new-branch>
Originally suggested:
Something like:
$ git checkout master
# remember where the master was referencing to
$ git branch previous_master
# Reset master back to origin/master
$ git reset --hard origin/master
with step 2 being optional.
Git supports this command:
git checkout -B master origin/master
Check out the origin/master
branch and then reset master
branch there.
I think even VonC's answer has complexity compared to this option:
git update-ref refs/heads/master origin/master
git reset --hard master
git automatically logs every value of a ref (through the reflog). So after you run that command, then master@{1}
refers to the previous value of master.
VonC's answer is correct, but it wastes time checkout out the old value of master into the filesystem.
If you care about orphaned objects in the repo, then you can run git gc