tags:

views:

177

answers:

4

How do I reset my local repository to be just like the remote repository HEAD? I did:

git reset -hard HEAD^

But when I do a git status,

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

Can you please tell me why I have these 'modified'? I haven't touched these files? If I did, I want to remove those.

+1  A: 

Was that a typo? It's git reset --hard HEAD not git reset -hard HEAD^, that is, if you have not committed the changes yet. I figured it was a typo, but just in case it isn't, here you go :)

But you can also just do what it says, to prevent possibly losing changes you did to other files that you do want to keep. So:

git reset HEAD java/com/mycompany/TestContacts.java
git reset HEAD java/com/mycompany/TestParser.java

Hope that works, sorry if I misunderstood your question.

Jorge Israel Peña
+1  A: 

git reset --hard HEAD actually only resets to the last committed state. In this case HEAD refers to the HEAD of your branch.

If you have several commits, this won't work..

What you probably want to do, is reset to the head of origin or whatever you remote repository is called. I'd probably just do something like

git reset --hard origin/HEAD

Be careful though. Hard resets cannot easily be undone. It is better to do as Dan suggests, and branch off a copy of your changes before resetting.

Mikael Ohlson
I was going to vote this up, except the revert information is wrong :( "git revert <commit>" will create a new commit that reverts the named commit. So doing "git revert origin/HEAD" will create a commit that *undoes* the last commit on "origin/HEAD" (also, usually you would use "origin/master" or some other branchname -- origin/HEAD would refer to the currently checked out branch in the origin repo, I believe... not something you'd likely want unless you know exactly which branch is currently checked out in the remote repo).
Dan Moulding
Ah, you are quite right! This is what I get from writing from memory, and not double checking! Will edit at once!
Mikael Ohlson
There was an incorrect suggestion in my answer that Dan caught earlier. I edited it away, since I don't want to lead anyone astray. As to the origin/master or origin/HEAD stuff, I expect that depends on whether or not you actually do a fetch first. If you just cloned origin, and it had no other branches, which I find to be quite common, then it should reset it fine. But of course, Dan is right.
Mikael Ohlson
+2  A: 

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

git fetch origin
git reset --hard origin/master

If you want to save your current repo'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 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).

Dan Moulding
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
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").
Dan Moulding
A: 

If you want to go back to the HEAD state for both the working directory and the index, then you should git reset --hard HEAD, rather than to HEAD^. (This may have been a typo, just like the single versus double dash for --hard.)

As for your specific question as to why those files appear in the status as modified, it looks like perhaps you did a soft reset instead of a hard reset. This will cause the files that were changed in the HEAD commit to appear as if they were staged, which is likely what you are seeing here.

Emil