63

I'm doing git pull of an open source lib, I don't care what my local copy is I just want to update it with the origin's version.

i.e. it can blow away my local changes.

3 Answers 3

99

This should do the trick:

git reset --hard HEAD
git pull
5
  • 50
    And if you've created any local commits that you want to throw away, use: git reset --hard origin/master instead. Commented Dec 29, 2010 at 2:07
  • 2
    And if you don't have custom local changes, you might want to do git pull --rebase
    – Henrik
    Commented Aug 9, 2011 at 7:40
  • 4
    doesn't work for me getting CONFLICTS and auto-merge fail messages. I don't care I just want to completely overwrite everything... Commented Oct 25, 2011 at 17:48
  • @schwiz You can delete the branch, or rename it, and recreate it to track from origin: stackoverflow.com/questions/2665045/…
    – Ian Hunter
    Commented Dec 19, 2011 at 21:25
  • 3
    This answer assumes that you have never made any commits to your branch, pretty far from what the question said ("I don't care what my local copy is").
    – jwg
    Commented Jan 16, 2013 at 13:40
39
git fetch
git checkout origin/name_of_branch  # correct state but in 'head without a name'
git checkout -B name_of_branch      # overwrite current tree onto name_of_branch

This checks out the remote tracking branch into a head without a name, then you can have a look and check you're happy. Whenever you want to (even after changes or commits) the second git checkout labels your current tree with 'name_of_branch', even if it has to delete the old name_of_branch to do so.


Edit: 'What a crazy command line syntax'

The syntax of git commands seems unintuitive. In fact it's the Git data model that is unintuitive. This isn't bad design, but because it works with files, branches, commits in ways that are much more flexible and powerful than other version control systems. Once you grok how these things work in Git, the command line will make a lot of sense and you will be able to accurately guess how to do complicated things.

  • git fetch This fetches all the updates that have happened on the origin server since the last time. Git separates fetching from the server from updating, merging, etc. any of your branches. All the branches called origin/XXX are your most recent versions of what's on the server. They are not remote branches but remote tracking branches, local branches which track remote branches. When you do git fetch, you update them, without touching any of your own branches. When you do git merge, git rebase, and so on, you use the fetched version, without grabbing a more recent copy from the server. This means you have control over when network requests are made (if you aren't always connected to the server), and you can 'fetch' a snapshot of the server, then merge at your leisure. If in doubt, do git fetch first.
  • git checkout origin/name_of_branch git checkout does two things. It updates your files to that branch, and it sets your HEAD to that branch. The files things is what you'd expect. The HEAD things means that when you do commits, they will added to the end of the branch that HEAD points to. IOW, checkout does both output - write the right versions of the files, and prepares for input - gets ready to store the commits you are going to do in the right place. If you checkout a branch called foo, your tree changes to the state saved in foo, and the next commits you do will be added to foo. In the case of origin\xyz, you can't write your changes to an origin branch - these track what is on the origin server and can't be committed to directly. So the checkout updates the files, and sets HEAD to nothing - an unnamed branch. This stops you accidentally committing your newly checked out stuff back onto the previous branch you were using, and in fact you will be heavily discouraged by git from committing at all until you have a destination branch.
  • git checkout -B name_of_branch As usual, when git does two different things with one command, both of them can be configured separately. So the second part of what checkout does, setting HEAD to the branch you want to commit to, can be to a new branch, instead of the branch you're checking out, if you use the -B option. So git checkout -B new_stuff old_stuff will set all your files to the state in old_stuff, but get you ready to write your commits to the new branch new_stuff. This is a common task, and saves you checking out a branch then forking it, in two steps. Now, almost all arguments to git commands can be omitted, and git will do the most obvious thing. In this case, that is making a new branch based on the one you are on, rather than one you want to checkout. So git takes the unnamed branch you are on, and makes a new branch called name_of_branch, which you can start committing to. Note that an uppper case letter "B" kind of means 'force'. If you used "-b" git would refuse to overwrite an already existing branch. With "-B" it will go ahead and do it without warning or confirming.
1
  • 4
    @cept0 if you read only several hundred words of heavily bulleted and bolded explanation, you too could learn to love git command syntax!
    – jwg
    Commented May 19, 2014 at 9:44
21

The go-to, knee-jerk, solution is: git reset --hard origin/master

† or origin/main or whatever the name of your origin's branch is.

It's the almighty solution for experts and beginners alike that swiftly gets the job done. Albeit while blowing away all uncommitted changes without warning.

The safer command is slightly more of a pain to type: git checkout -B master origin/master

Enter aliases:

git config --global alias.become '!git checkout -B "$(git symbolic-ref --short HEAD)"'

Henceforth, one can type: git become origin/master

4
  • origin/master gives error message. origin/main works. Additionally the folders and files have current date/time, not the date/time in the repo. Commented Dec 4, 2021 at 22:50
  • Updated to clarify branch name. The thing about date/time sounds like a bad expectation as git doesn't record timestamps of files or folders.
    – antak
    Commented Dec 6, 2021 at 3:42
  • I beg to differ on the timestamps. Git tells you how many minutes, hours, days, months or years ago a file was last changed. That is only possible with a timestamp on each file. Commented Dec 6, 2021 at 11:52
  • @WinEunuuchs2Unix That info in visualizations such as in GitHub is based on the timestamp of the commit. It's not the timestamp of the file. Git isn't concerned about file-system timestamps and neither records or makes attempts to restore said timestamps when checking out files.
    – antak
    Commented Dec 7, 2021 at 9:18

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