11

I mistakenly added a directory to git and when I followed the tip here to undo the add by doing git reset HEAD <file>, I was horrified to discover that the current working copy of one of the files, which has lots of changes (work!) in it, reverted back to the previous version!

As a result I lost several hours worth of work... :((

I thought that git reset HEAD <file> only "removes it from the current index without changing anything else. What did I miss?

Is git reset HEAD <file> supposed to also check out the file from HEAD?

How can I minimize the chances of something like this happening again in the future?

2 Answers 2

20

Only git checkout -- <file> should have reverted the files in their previous stats. git reset HEAD <file> should only unstage the file, not revert its content.

2
  • OK, could a git rm -r <directory> that I did earlier (and git reported that this operation failed) explain what happened? Commented Jul 22, 2011 at 12:56
  • git reset HEAD --hard will reverted the files in their previous stats :]
    – MichaelMao
    Commented Jul 8, 2019 at 0:53
2

git reset unstages files from index. Maybe you added --hard option or used git checkout afterwards?

Quoting the git-reset manpage:

git reset [-q] [<commit>] [--] <paths>... This form resets the index entries for all to their state at . (It does not affect the working tree, nor the current branch.)

3
  • Thanks. Do I understand you correctly that git reset HEAD <file> is NOT supposed to do what I experienced? i.e. it was something else that I did that reverted the file back? Commented Jul 22, 2011 at 12:54
  • @Eternal Learner - Yes, perhaps reading man git-reset would be a good thing to do :) Commented Jul 22, 2011 at 12:56
  • I read it countless times before. Apparently it didn't help me this time. :( Commented Jul 22, 2011 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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