Can I do the following in a simpler way?

git checkout origin/master
git branch -D master
git branch master
git checkout master

Git supports this command:

git checkout -B master origin/master

Check out the origin/master branch and then reset master branch there.

  • 5
    The only true answer. Jan 8 '16 at 12:14
  • 3
    save four keystrokes -- you don't need the quotes. Just: git checkout -B master origin/master Mar 24 '16 at 2:29
  • let's says I commited 2 things, the first one is a merge with branch and the second one is regular. What happens to the merge if I go back to the origin/master?
    – utdev
    Dec 22 '16 at 15:05
  • 1
    don't you have to git fetch origin master before to be sure origin/master is updated?
    – pedrozath
    Mar 29 '17 at 16:22
  • Yes, of course with all solutions you should do git fetch first
    – KindDragon
    Mar 29 '17 at 17:03

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.

  • 1
    You can do that with one line. Jan 8 '16 at 12:17
  • Thank you VonC, I have updated to accept @KindDragon's answer as dear of you suggested. The technique is always advancing. It's already 10 YEARS!! Love you guys!! ^_^ Jul 22 '20 at 12:17
  • @XièJìléi No problem, good choice. Mine adds more details to KindDragon's.
    – VonC
    Jul 22 '20 at 12:33

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

  • 1
    Sounds an interesting alternative. +1
    – VonC
    Mar 19 '13 at 12:37
  • I still get Already on 'master' Aug 21 '13 at 22:56
  • @yourfriendzak, I forgot about taking into account that you might already have master checked out before updating master. I have updated the answer to be one that should work even in that case as well. Aug 22 '13 at 19:02
  • This works even if you're not on master (like a detached HEAD state that is actually pointing to the tip origin/master). Then, you can checkout master without having to flip old files through the repo. Great!
    – Andrew Mao
    Jul 25 '14 at 19:26

If you are already on master you can do the following:

git reset --hard origin/master

It will point the local master branch to the remote origin/master and discard any modifications in the working dir.

  • 2
    And will delete files! If you have created/edited files, and have run a "git add" on them, this command will delete them. Be warned.
    – Cheeso
    Nov 11 '15 at 15:30
  • Is this approach better than git checkout -B master origin/master ?
    – Jim Aho
    Jun 2 '19 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.