Categories
git undo

Reset local repository branch to be just like remote repository HEAD

5063

How do I reset my local branch to be just like the branch on the remote repository?

I did:

git reset --hard HEAD

But git status claims I have modified files:

On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
      modified:   java/com/mycompany/TestContacts.java
      modified:   java/com/mycompany/TestParser.java

5

  • 14

    According to the output of git status your second command git reset --hard HEAD failed. You didn’t paste it’s output, though. → Incomplete question.

    Jan 8, 2016 at 11:06

  • 4

    You are mixing two issues here: 1) how to reset a local branch to the point where the remote is and 2) how to clear your staging area (and possibly the working directory), so that git status says nothing to commit, working directory clean. – Please specify!

    Jan 8, 2016 at 11:09


  • 2

    Does this answer your question? How do I force “git pull” to overwrite local files?

    – Henke

    Dec 28, 2020 at 15:51

  • Obviously not an answer if the repo is large, but for small repos you can hit it with a hammer and avoid git entirely: rm -fr ./repo; git clone repo. best way i’ve found

    Jun 8, 2021 at 17:08

  • YOu save my life …. Love you… I was check out to another branch which was empty… neither checkout happen… bythis time my 1 week work was gone… and no way i can reach deadline. Thanks God!

    – Rajib

    Jun 22 at 12:40

8753

Setting your branch to exactly match the remote branch can be done in two steps:

git fetch origin
git reset --hard origin/master

If you want to save your current branch’s state before doing this (just in case), you can do:

git commit -a -m "Saving my work, just in case"
git branch my-saved-work

Now your work is saved on the branch “my-saved-work” in case you decide you want it back (or want to look at it later or diff it against your updated branch).

Note that the first example assumes that the remote repo’s name is “origin” and that the branch named “master” in the remote repo matches the currently checked-out branch in your local repo.

BTW, this situation that you’re in looks an awful lot like a common case where a push has been done into the currently checked out branch of a non-bare repository. Did you recently push into your local repo? If not, then no worries — something else must have caused these files to unexpectedly end up modified. Otherwise, you should be aware that it’s not recommended to push into a non-bare repository (and not into the currently checked-out branch, in particular).

20

  • 4

    Thank you for your answer. You said ‘Note that the first example assumes that the remote repo’s name is “origin” and that the branch named “master” in the remote repo matches the branch in your local repo.’ How can I double check my remote repo’s name and my branch name to be sure before I execute ‘git reset –hard’? Thanks again.

    – hap497

    Oct 27, 2009 at 2:57

  • 27

    If you didn’t explicitly name the remote, then it’s name is likely just “origin” (the default). You can use “git remote” to get a list of all remote names. You can then use “git remote <name>” to see which branches push/pull with each other (e.g. if your “master” branch was cloned from “master” in the remote named “origin”, then you’ll get a line that says “master merges with remote master”).

    Oct 27, 2009 at 13:51

  • 7

    “it’s not recommended to push into a non-bare repository (and not into the currently checked-out branch, in particular” Why is that?

    Mar 20, 2013 at 11:00

  • 34

    Just after fetching, I believe you can do git reset FETCH_HEAD --hard instead, as well, that’s the same meaning.

    – Jean

    Apr 30, 2013 at 21:21

  • 9

    It did not remove files I have added.

    Mar 20, 2018 at 9:01

587

I needed to do (the solution in the accepted answer):

git fetch origin
git reset --hard origin/master

Followed by:

git clean -f

to remove local files

To see what files will be removed (without actually removing them):

git clean -n -f

10

  • 109

    also, git clean -d -f if there are untracked directories present.

    – garg

    Jan 23, 2015 at 18:09

  • 67

    also git clean -fdx

    Sep 28, 2015 at 6:32

  • 19

    If you want exact copy of remote branch you have to follow by git clean -ffdx. Note that thare are two f.

    Feb 22, 2016 at 14:51

  • 8

    The git clean -f was the essential piece I needed. Thanks!

    – dgo

    Apr 26, 2016 at 22:51

  • 21

    be careful using the clean command. it can delete ignored files from other branches.

    – mikoop

    Jan 9, 2017 at 8:23


519

First, use git reset to reset to the previously fetched HEAD of the corresponding upstream branch:

git reset --hard @{u}

The advantage of specifying @{u} or its verbose form @{upstream} is that the name of the remote repo and branch don’t have to be explicitly specified. On Windows or with PowerShell, specify "@{u}" (with double quotes).

Next, as needed, use git clean to remove untracked files, optionally also with -x:

git clean -df

Finally, as needed, get the latest changes:

git pull

10

  • 83

    This seems like a better answer than the accepted one, because it dynamically resets to the current upstream branch rather than always a static one such as origin/master

    – Jon z

    Sep 22, 2015 at 15:10

  • 2

    @GangadharJannu git reset --hard requires a commit, else it wouldn’t know what to reset you to. @{u} points to a specific commit – the head of the tracked branch, from when you last did a git fetch.

    Oct 16, 2019 at 7:06

  • 1

    @KristofferBakkejord Thanks for the explanation but even without commit hash we can do git reset --hard though it will not reset to remote branch

    – Buddybear

    Oct 16, 2019 at 8:42

  • 11

    For anyone else who almost opened a new question here, if you git from Powershell, use quotes (git reset --hard "@{u}"). Took me a while to figure that out.

    Dec 13, 2019 at 14:26

  • 1

    @MPStoering using the quotes also worked for me in git bash on Windows. Cheers

    – Jason S

    Jul 13, 2020 at 22:03