Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git status shows modifications, git checkout -- <file> doesn't remove them

People also ask

Does git checkout remove changes?

git clean removes all untracked files and git checkout clears all unstaged changes.

How do I force git checkout?

Force a Checkout You can pass the -f or --force option with the git checkout command to force Git to switch branches, even if you have un-staged changes (in other words, the index of the working tree differs from HEAD ). Basically, it can be used to throw away local changes.


I was having this problem on Windows but wasn't prepared to look into the ramifications of using config --global core.autocrlf false I also wasn't prepared to abandon other private branches and goodies in my stash and start with a fresh clone. I just need to get something done. Now.

This worked for me, on the idea that you let git rewrite your working directory completely:

git rm --cached -r .
git reset --hard

(Note that running just git reset --hard wasn't good enough nor was a plain rm on the files before the reset as are suggested in the comments to the original question)


There are multiple problems the can cause this behaviour:

Line ending normalization

I've had these kinds of problems too. It comes down to git automatically converting crlf to lf. This is typically caused by mixed line endings in a single file. The file gets normalized in the index, but when git then denormalizes it again to diff it against the file in the working tree, the result is different.

But if you want to fix this, you should disable core.autocrlf, change all line endings to lf, and then enable it again. Or you can disable it altogether by doing:

git config --global core.autocrlf false

Instead of core.autocrlf, you can also consider using .gitattribute files. This way, you can make sure everyone using the repo uses the same normalization rules, preventing mixed line endings getting into the repository.

Also consider setting core.safecrlf to warn if you want git to warn you when a non-reversible normalization would be performed.

The git manpages say this:

CRLF conversion bears a slight chance of corrupting data. autocrlf=true will convert CRLF to LF during commit and LF to CRLF during checkout. A file that contains a mixture of LF and CRLF before the commit cannot be recreated by git. For text files this is the right thing to do: it corrects line endings such that we have only LF line endings in the repository. But for binary files that are accidentally classified as text the conversion can corrupt data.

Case-insensitive file systems

On case-insensitive filesystems, when the same filename with different casing is in the repository, git tries to checkout both, but only one ends up on the file system. When git tries to compare the second one, it would compare it to the wrong file.

The solution would either be switching to a non-case insensitive filesystem, but this in most cases is not feasible or renaming and committing one of the files on another filesystem.


Another solution that may work for people, since none of the text options worked for me:

  1. Replace the content of .gitattributes with a single line: * binary. This tells git to treat every file as a binary file that it can't do anything with.
  2. Check that message for the offending files is gone; if it's not you can git checkout -- <files> to restore them to the repository version
  3. git checkout -- .gitattributes to restore the .gitattributes file to its initial state
  4. Check that the files are still not marked as changed.

For future people having this problem: Having filemode changes can also have the same symptoms. git config core.filemode false will fix it.


This has been driving me crazy, especially that I couldn`t fix this without any of the solutions found online. Here is how I solved it. Can't take the credits here since this is the work of a colleague :)

Source of the problem: My initial installation of git was without auto line conversion on windows. This caused my initial commit to GLFW to be without the proper line ending.

Note: This is only a local solution. The next guy cloning the repo will still be stuck with this problem. A permanent solution can be found here: https://help.github.com/articles/dealing-with-line-endings/#re-normalizing-a-repository.

Setup: Xubuntu 12.04 Git repo with glfw project

Problem: Unable to reset glfw files. They always show as modified, regardless of what I tried.

Solved:

edit .gitattributes

Comment out the line:    # text=auto

Save the file

restore .gitattributes:   git checkout .gitattributes

I had a .bat file with the same problem (couldn't get rid it it in untracked files). git checkout -- didn't work, neither did any of the suggestions on this page. The only thing that worked for me was to do:

git stash save --keep-index

And then to delete the stash:

git stash drop

Got the same issue twice! Both times when stash some changes I made and then tried to pop them back. Could'nt pop the changes since I've got lots of file that are changed -- but they are NOT! They are EXACTLY the same.

I now think I've tried all the above solutions without success. After trying the

git rm --cached -r .
git reset --hard

I now got almost all the files in my repository modified.

When diffing the file, it says I've deleted all the lines and then add them again.

Kind of disturbing. I will now avoid stashing in the future..

The only solution is to clone a new repository and start over. (Made it last time)