How do I reset 'master' to 'origin/master'?

235

70

Can I do the following in a simpler way?

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

Xiè Jìléi

Posted 2011-04-20T10:34:06.907

Reputation: 14 766

Sometimes, this can be done without touching the working tree: http://stackoverflow.com/a/12343727/586086

– Andrew Mao – 2014-09-08T19:08:06.210

6Please update your accepted answer: @KindDragon’s answer is correcter and shorter. – Robert Siemer – 2016-01-08T12:17:36.260

Answers

343

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.

VonC

Posted 2011-04-20T10:34:06.907

Reputation: 13 292

1You can do that with one line. – Robert Siemer – 2016-01-08T12:17:01.597

102

Git supports this command:

git checkout -B master origin/master

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

KindDragon

Posted 2011-04-20T10:34:06.907

Reputation: 1 151

4The only true answer. – Robert Siemer – 2016-01-08T12:14:04.017

3save four keystrokes -- you don't need the quotes. Just: git checkout -B master origin/master – zumalifeguard – 2016-03-24T02:29:39.120

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 – 2016-12-22T15:05:34.063

1don't you have to git fetch origin master before to be sure origin/master is updated? – pedrozath – 2017-03-29T16:22:52.757

Yes, of course with all solutions you should do git fetch first – KindDragon – 2017-03-29T17:03:48.710

won't this lose commits on the local master as you reset it to origin/master? usually people get to this point when they committed stuff directly onto master by mistake, and wish they had branched previously. – Motti Shneor – 2019-06-12T15:47:11.347

@MottiShneor: Yes, it will lose all new activity on local master. Which is what the question asks for. – Ben Voigt – 2019-12-18T15:34:42.050

30

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

Alexander Bird

Posted 2011-04-20T10:34:06.907

Reputation: 1 697

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 – 2014-07-25T19:26:33.447

1Sounds an interesting alternative. +1 – VonC – 2013-03-19T12:37:46.590

I still get Already on 'master' – yourfriendzak – 2013-08-21T22:56:00.373

@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 – 2013-08-22T19:02:11.640

22

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.

Fuad Saud

Posted 2011-04-20T10:34:06.907

Reputation: 361

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 – 2015-11-11T15:30:57.967

Is this approach better than git checkout -B master origin/master ? – Jim Aho – 2019-06-02T18:02:07.153