Can I do the following in a simpler way?

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

4 Answers 4


Git supports this command:

git checkout -B master origin/master

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

Or you can use new switch command for that

git switch -C master origin/master
  • 6
    The only true answer. Commented Jan 8, 2016 at 12:14
  • 3
    save four keystrokes -- you don't need the quotes. Just: git checkout -B master origin/master Commented Mar 24, 2016 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
    Commented Dec 22, 2016 at 15:05
  • 1
    don't you have to git fetch origin master before to be sure origin/master is updated?
    – pedrozath
    Commented Mar 29, 2017 at 16:22
  • Yes, of course with all solutions you should do git fetch first
    – KindDragon
    Commented Mar 29, 2017 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. Commented Jan 8, 2016 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!! ^_^
    – Lenik
    Commented Jul 22, 2020 at 12:17
  • @XièJìléi No problem, good choice. Mine adds more details to KindDragon's.
    – VonC
    Commented Jul 22, 2020 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
    Commented Mar 19, 2013 at 12:37
  • I still get Already on 'master' Commented Aug 21, 2013 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. Commented Aug 22, 2013 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
    Commented Jul 25, 2014 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
    Commented Nov 11, 2015 at 15:30
  • Is this approach better than git checkout -B master origin/master ?
    – Jim Aho
    Commented Jun 2, 2019 at 18:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .