217

Can I do the following in a simpler way?

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

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>

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. – Robert Siemer Jan 8 '16 at 12:17
95

Git supports this command:

git checkout -B master origin/master

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

  • 4
    The only true answer. – Robert Siemer Jan 8 '16 at 12:14
  • 3
    save four keystrokes -- you don't need the quotes. Just: git checkout -B master origin/master – zumalifeguard 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
29

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' – yourfriendzak 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. – Alexander Bird 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
20

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.

  • 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 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.