Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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: stackoverflow.com/a/12343727/586086 –  Andrew Mao Sep 8 at 19:08

5 Answers 5

up vote 63 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.

share|improve this answer
    
The most useful part for me was step 2. I found that git didn't want to point master at origin/master until there was some branch pointing at the previous master. –  sidewaysmilk Aug 6 '12 at 21:12

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 at 19:26

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

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

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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