Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unstaged changes left after git reset --hard

Tags:

git

git-reset

After git reset --hard, git status gives me files within the Changes not staged for commit: section.

I've also tried git reset ., git checkout -- . and git checkout-index -f -a, to no avail.

So, how can I get rid of those unstaged changes?

This seems to hit only Visual Studio project files. Weird. See this paste: http://pastebin.com/eFZwPn9Z. What is special with those files, is that in .gitattributes I have:

*.sln        eol=crlf
*.vcproj     eol=crlf
*.vcxproj*   eol=crlf

Also, autocrlf is set to false in my global .gitconfig. Could that be somehow relevant?

like image 540
Norswap Avatar asked Jul 08 '12 12:07

Norswap


People also ask

Does git reset hard remove unstaged files?

git reset --hard resets your index and reverts the tracked files back to state as they are in HEAD. It leaves untracked files alone.

How do I get my changes back after resetting git hard?

Suppose you staged a file with git add <file-name> and then did a hard reset with git reset --hard HEAD before committing. Afterward, you found out that the staged file is missing. In this case, also, you can recover the files. We can use the command git fsck to recover the files after a hard reset.

What after git reset hard?

If you do git reset --hard <SOME-COMMIT> then Git will: Make your current branch (typically master ) back to point at <SOME-COMMIT> . Then make the files in your working tree and the index ("staging area") the same as the versions committed in <SOME-COMMIT> .


5 Answers

I had the same problem and it was related to the .gitattributes file. However the file type that caused the problem was not specified in the .gitattributes.

I was able to solve the issue by simply running

git rm .gitattributes
git add -A
git reset --hard
like image 144
GameScripting Avatar answered Oct 23 '22 03:10

GameScripting


I have resolved this problem using following steps

  1. Remove every file from Git's index.

    git rm --cached -r .
    
  2. Rewrite the Git index to pick up all the new line endings.

    git reset --hard
    

Solution was part of steps described on Configuring Git to handle line endings

like image 41
Jacek Szybisz Avatar answered Oct 23 '22 03:10

Jacek Szybisz


Git won't reset files that aren't on repository. So, you can:

$ git add .
$ git reset --hard

This will stage all changes, which will cause Git to be aware of those files, and then reset them.

If this does not work, you can try to stash and drop your changes:

$ git stash
$ git stash drop
like image 29
YuriAlbuquerque Avatar answered Oct 23 '22 02:10

YuriAlbuquerque


If you use Git for Windows, this is likely your issue

I've had the same problem and stash, hard reset, clean or even all of them was still leaving changes behind. What turned out to be the problem was the x file mode that was not set properly by git. This is a "known issue" with git for windows. The local changes show in gitk and git status as old mode 100755 new mode 100644, without any actual file differences.

The fix is to ignore the file mode:

git config core.filemode false

More info here

like image 21
vezenkov Avatar answered Oct 23 '22 04:10

vezenkov


Okay, I've kind of solved the problem.

It seemed that the .gitattributes file, containing:

*.sln        eol=crlf
*.vcproj     eol=crlf
*.vcxproj*   eol=crlf

made the project files appear unstaged. I am clueless why that is, and I'm really hoping that someone privy to the ways of git will give us a nice explanation.

My fix was to remove these files, and to add autocrlf = false under [core] in .git/config.

This does not amount to exactly the same thing as the previous configuration, as it requires every dev to have autocrlf = false. I'd like to find a better fix.

EDIT:

I commented the incriminating lines, uncommented them and it worked. What the ... I don't even ... !

like image 34
Norswap Avatar answered Oct 23 '22 04:10

Norswap