up vote 4300 down vote accepted

Important: If you have any local changes, they will be lost. With or without --hard option, any local commits that haven't been pushed will be lost.[*]

If you have any files that are not tracked by Git (e.g. uploaded user content), these files will not be affected.


I think this is the right way:

git fetch --all
git reset --hard origin/master

OR If you are on some other branch

git reset --hard origin/your_branch

Explanation:

git fetch downloads the latest from remote without trying to merge or rebase anything.

Then the git reset resets the master branch to what you just fetched. The --hard option changes all the files in your working tree to match the files in origin/master


[*]: It's worth noting that it is possible to maintain current local commits by creating a branch from master before resetting:

git checkout master
git branch new-branch-to-save-current-commits
git fetch --all
git reset --hard origin/master

After this, all of the old commits will be kept in new-branch-to-save-current-commits. Uncommitted changes however (even staged), will be lost. Make sure to stash and commit anything you need.

share|improve this answer
82  
This is the correct answer, that will properly make the local repository exactly match the remote tracking branch. – Ether Feb 17 '12 at 17:25
57  
I can confirm that this is likely what you want when you've totally hosed your local repository and don't care about any local commits and you just want whatever was in your remote because you know it's good. – seaneshbaugh Jun 14 '12 at 5:58
307  
It's a popular question, so I'd like to clarify on the top comment here. I just executed commands as described in this answer and it hasn't removed ALL the local files. Only the remotely tracked files were overwritten, and every local file that has been here was left untouched. – Red Nov 22 '12 at 10:38
7  
This worked for me and my local files were NOT deleted. – Tastybrownies May 21 '13 at 18:16
5  
in case you're pulling from a repo that has its remote branch name different from "master", use git reset --hard origin/branch-name – Nerrve Dec 17 '13 at 11:17

Try this:

git reset --hard HEAD
git pull

Should do what you want.

share|improve this answer
8  
I've done this and some local files that were no longer in repo were left on the disk. – Piotr Owsiak Apr 8 '11 at 16:00
12  
I do not think that this is correct. the above will perform a merge, not overwrite which was requested in the question: "How to force git to overwrite them?" I do not have the answer, I am currently looking for it.. at the moment I switch to the branch with with the code that I want to keep "git checkout BranchWithCodeToKeep", then do "git branch -D BranchToOverwrite" and then finally "git checkout -b BranchToOverwrite". you will now have the exact code from BranchWithCodeToKeep on the branch BranchToOverwrite without having to perform a merge. – Felbus Jul 13 '11 at 10:11
190  
instead of merging using 'git pull', try git fetch --all followed by 'git reset --hard origin/master' – Lloyd Moore Feb 21 '12 at 14:56
20  
Lloyd Moore's suggestion works, Travis R's answer does not work. – dwelch Jun 29 '12 at 21:47
1  
yep, the @lloydmoore solution worked for me. Could do with being an answer rather than just a comment. – Max Williams Nov 19 '12 at 9:54