Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Can I do following in a simpler way?

git checkout origin/master
git branch -D master
git branch master
git checkout master
share|improve this question
Sometimes, this can be done without touching the working tree: – Andrew Mao Sep 8 '14 at 19:08
Please update your accepted answer: @KindDragon’s answer is correcter and shorter. – Robert Siemer Jan 8 at 12:17
up vote 112 down vote accepted

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.

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>
share|improve this answer
You can do that with one line. – Robert Siemer Jan 8 at 12:17
Shouldn't it be git checkout -B master origin/master (without the backticks and colon)? – cwohlman Jun 13 at 15:56
@cwohlman yes it should. I have edited the answer. – VonC Jun 13 at 16:44

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

share|improve this answer
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

Git supports this command:

git checkout -B master origin/master

Checkout the origin/master branch and then reset master branch there

share|improve this answer
The only true answer. – Robert Siemer Jan 8 at 12:14
save four keystrokes -- you don't need the quotes. Just: git checkout -B master origin/master – zumalifeguard Mar 24 at 2:29

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.

share|improve this answer
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

You already are the master. What you want is to remove the remote origin so git pull doesn't do anything. So

git remote rm origin

You can then add the current repository as the origin to the remote repository if you feel like it.

NB if local is ahead of remote your solution will wipe local changes!

share|improve this answer

You must log in to answer this question.

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