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?

Community
  • 1
  • 1
Eternal Learner
  • 2,602
  • 5
  • 27
  • 38

2 Answers2

19

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.

demental
  • 1,444
  • 13
  • 25
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.)

Rafał Rawicki
  • 22,324
  • 5
  • 59
  • 79