64

How do I reset my local git repo to be exactly the same as the remote repo?

I've tried:

git reset --hard HEAD^

But now git status says I have diverging commits. I basically want to just wipe anything I've got locally and get the exact remote repo on my local machine.

James
  • 109,676
  • 31
  • 162
  • 175
  • 1
    possible duplicate of [How to reset my local repository to be just like the remote repository HEAD](http://stackoverflow.com/questions/1628088/how-to-reset-my-local-repository-to-be-just-like-the-remote-repository-head) – CharlesB Apr 16 '12 at 07:34
  • Does this answer your question? [Reset local repository branch to be just like remote repository HEAD](https://stackoverflow.com/questions/1628088/reset-local-repository-branch-to-be-just-like-remote-repository-head) – Kay V Mar 08 '21 at 03:28

2 Answers2

135

git reset --hard HEAD^ will only reset your working copy to the previous (parent) commit. Instead, you want to run

git reset --hard origin/master

Assuming remote is origin and the branch you want to reset to is master

CharlesB
  • 86,532
  • 28
  • 194
  • 218
  • 1
    in my case I also need to run "git pull" to point my local master correctly. otherwhise my local would stay in detached mode – yigal Dec 29 '20 at 18:47
  • 1
    If you want to remove local untracked files, you can run `git clean` first. Run `git clean -n` to see what would happen (i.e. which files would be removed), and `git clean -f` to actually remove them. – f.k May 02 '21 at 14:23
  • This will only reset the given branch. Not the full repo as was asked by OP. Just to be aware. – Ivan Balashov Dec 27 '21 at 08:09
  • Is it possible to revert this command? – Ivana Murray May 29 '23 at 09:50
  • @IvanaMurray yes with the `git reflog` command you can see the history of the HEAD, and pass it to `git reset --hard` as well – CharlesB May 30 '23 at 13:56
16

You could delete the current branch, and create the branch again at the remote/branchname commit

git branch -D branchname
git checkout remote/branchname
git branch branchname
Sailesh
  • 25,517
  • 4
  • 34
  • 47