413

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?

8
  • Did you apply those commands from the root of the repository? Notice . stands for current directory not root directory
    – Alexander
    Jul 8, 2012 at 12:24
  • 1
    I did do them from the root repository indeed.
    – Norswap
    Jul 8, 2012 at 12:25
  • What is your git version? Either you are making a silly mistake or you have an old version that is buggy?
    – Shahbaz
    Jul 8, 2012 at 12:27
  • It's git 1.7.4 msysgit. It might be a mistake, but the commands seem simple enough, and I couldn't spot a mistake.
    – Norswap
    Jul 8, 2012 at 12:29
  • For the record, I tried using the latest version of msysgit (1.7.11), but the problem persists.
    – Norswap
    Jul 8, 2012 at 12:38

22 Answers 22

471

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
14
  • 9
    It's the * text=auto directive in the gitattributes file. It automatically changes file endings, so the files will automatically be marked as "modified" when you check the status. Feb 19, 2015 at 22:24
  • 4
    This works, but I do not exactly understand why. Anyone care to explain each command? Mar 20, 2015 at 14:04
  • 28
    @NLwino, git rm .gitattributes removes .gitattributes from the index. git add -A adds all (including the removal of .gitattributes) to the index, which should then only be the removal of .gitattributes, if that was really the problem. git reset --hard resets all uncommitted changes, which would include the removal of .gitattributes. Essentially, this ignores .gitattributes (and the * text=auto directive) for one commit by deleting it, then resetting the index while it's deleted, therefore putting it back, but after you've already reset the other files, so they weren't modified again.
    – cloudworks
    Aug 22, 2015 at 7:12
  • 5
    @GameScripting, This solution doesn't work for me. There is no such file as .gitattributes: "fatal: pathspec '.gitattributes' did not match any files"
    – Pacerier
    Oct 20, 2015 at 9:03
  • 5
    I do such and it says fatal: pathspec '.gitattributes' did not match any files . What am I doing wrong?
    – mfaani
    Oct 10, 2016 at 16:33
289

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

9
  • 8
    THIS WORKS when nothing else does! We've tried everything in this and many other threads -- it's a big development team so getting everyone on the same crlf has been a nightmare, but this solves the problem.
    – tkersh
    Jan 12, 2017 at 1:32
  • 2
    Interesting ... I used "git rm --chached <one_specific_file>, instead of deleting everything. "git status" reported that file as deleted and untracked. Then "git reset --hard" fix that file and all the other files that were being reported as "Changes not staged". (Before using git rm, I hard tried reset hard to no effect). I love such mysterious source control systems.
    – joeking
    Feb 17, 2017 at 20:15
  • 3
    That is brutal. You could have done the same thing by deleting the repo directory and re-cloning.
    – ingyhere
    Mar 3, 2018 at 0:55
  • 5
    Consider that you are on Windows and that it is case-insensitive. If you are also working with developers who are on case-sensitive file systems, like Macs or Linux, they may be committing tags, branches or even files in different cases. In that situation your index will show files existing that are overwritten by the OS when it pulls. The only way to fix that would be to institute a naming policy on your Git server (hooks) or sniff out errors in advance.
    – ingyhere
    Mar 3, 2018 at 0:58
  • 7
    This does not do the trick for me. I needed to do git add . before git reset --hard Mar 20, 2020 at 9:00
134

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
7
  • 4
    The files were already tracked. Tried it anyway and it doesn't work either. There must be something really wrong with my repo, but I have no clue what. For the git stash thingy, see my answer to Shahbaz.
    – Norswap
    Jul 8, 2012 at 12:28
  • What is happening when you do a git status? Jul 8, 2012 at 12:29
  • 1
    I thought about a solution. Make a commit and a interactive rebase to erase that commit. Jul 8, 2012 at 15:00
  • I tried that at one point, and it worked, tough I later gave it one more try and came up with the surprising result outlined in the edit of my answer.
    – Norswap
    Jul 8, 2012 at 15:20
  • 1
    @YuriAlbuquerque, Git does not quite understand POLA. Isn't the whole point of git reset --hard to "reset both the staging area and the working directory to match"? If a file is not staged, obviously we'd want to remove it when we do reset --hard.
    – Pacerier
    Oct 20, 2015 at 9:06
129

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

3
  • Thanks. This was helpful. See also stackoverflow.com/a/31187212/470749
    – Ryan
    Jul 6, 2020 at 21:32
  • This worked for me also when the repository was stored in a NAS with a different filesystem/os than the computer running git (no Windows involved)
    – furins
    Aug 10, 2023 at 8:41
  • This fixed it for me using WSL on Windows
    – Arturo
    Oct 27, 2023 at 21:14
35

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 ... !

5
  • 1
    I just had this same exact problem occur and this was the only solution that worked. In my case I switched from a feature branch to my master and pulled some new changes from upstream and it just started happening for 2 files that were modified. It looks like the files may have been switched from Unix to Windows line endings which may have something to do with it. Not sure how or why though. Feb 7, 2013 at 19:25
  • +1 Same issue on Windows Git. Already had autocrlf = false. I merged in changes from upstream svn repo (git svn fetch / git merge git-svn) and was left with 1 file marked as modified. The strange thing is that I've had this issue before and all I needed to do was check out another branch (with the -f flag), and then switch back. I did that, and it worked, but when I switched to a feature branch, the "change" was back! Your edit at the bottom of the answer was the solution. In my case I commented/uncommented one line at the top of my .gitattributes file: * text=auto !eol Oct 9, 2013 at 18:53
  • 1
    That EDIT worked for me as well: comment out the lines in .gitattributes, run git status, uncomment the lines in .gitattributes, run git status again
    – Ignitor
    Aug 8, 2016 at 12:47
  • This is because git is resetting those files, then applying the line endings specified in the .gitattributes again. The git diff probably shows the famous wall of red/wall of green that is typical of line-ending differences.
    – Dagrooms
    Jul 10, 2017 at 16:57
  • My solution: Before you start: Commit your current work. Then: 1. git reset --hard or git checkout --force <branch> 2. git add --all --renormalize 3. git commit -m "Normalize all the line endings". Reference
    – ADTC
    Dec 1, 2021 at 16:26
31

Possible Cause #1 - Line Ending Normalization

One situation in which this can happen is when the file in question was checked into the repository without the correct configuration for line endings (1) resulting in a file in the repository with either the incorrect line endings, or mixed line endings. To confirm, verify that git diff shows only changes in line endings (these may not be visible by default, try git diff | cat -v to see carriage returns as literal ^M characters).

Subsequently, someone probably added a .gitattributes or modified the core.autocrlf setting to normalize line endings (2). Based on the .gitattributes or global config, Git has applied local changes to your working copy that apply the line ending normalization requested. Unfortunately, for some reason git reset --hard does not undo these line normalization changes.

Solution

Workarounds in which the local line endings are reset will not solve the problem. Every time the file is "seen" by git, it will try and reapply the normalization, resulting in the same issue.

The best option is to let git apply the normalization it wants to by normalizing all the line endings in the repo to match the .gitattributes, and committing those changes -- see Trying to fix line-endings with git filter-branch, but having no luck.

If you really want to try and revert the changes to the file manually then the easiest solution seems to be to erase the modified files, and then to tell git to restore them, although I note this solution does not seem to work consistently 100% of the time (WARNING: DO NOT run this if your modified files have changes other than line endings!!):

    git status --porcelain | grep "^ M" | cut -c4- | xargs rm
    git checkout -- .

Note that, unless you do normalize the line endings in the repository at some point, you will keep running into this issue.

Possible Cause #2 - Case Insensitivity

The second possible cause is case insensitivity on Windows or Mac OS/X. For example, say a path like the following exists in the repository:

/foo/bar

Now someone on Linux commits files into /foo/Bar (probably due to a build tool or something that created that directory) and pushes. On Linux, this is actually now two separate directories:

/foo/bar/fileA
/foo/Bar/fileA

Checking this repo out on Windows or Mac may result in modified fileA that cannot be reset, because on each reset, git on Windows checks out /foo/bar/fileA, and then because Windows is case insensitive, overwrites the content of fileA with /foo/Bar/fileA, resulting in them being "modified".

Another case may be an individual file(s) that exists in the repo, which when checked out on a case insensitive filesystem, would overlap. For example:

/foo/bar/fileA
/foo/bar/filea

There may be other similar situations that could cause such problems.

git on case insensitive filesystems should really detect this situation and show a useful warning message, but it currently does not (this may change in the future -- see this discussion and related proposed patches on the git.git mailing list).

Solution

The solution is to bring the case of files in the git index and the case on the Windows filesystem into alignment. This can either be done on Linux which will show the true state of things, OR on Windows with very useful open source utility Git-Unite. Git-Unite will apply the necessary case changes to the git index, which can then be committed to the repo.

Another useful tool with Git 2.16+ is the git add --renormalize <pathspec> command. See https://stackoverflow.com/a/47580886/430128 and the git-add man page for more information:

--renormalize

core.autocrlf configuration or the text attribute in order to correct files added with wrong CRLF/LF line endings. This option implies -u. Lone CR characters are untouched, thus while a CRLF cleans to LF, a CRCRLF sequence is only partially cleaned to CRLF.> Apply the "clean" process freshly to all tracked files to forcibly add them again to the index. This is useful after changing

(1) This was most likely by someone using Windows, without any .gitattributes definition for the file in question, and using the default global setting for core.autocrlf which is false (see (2)).

(2) http://adaptivepatchwork.com/2012/03/01/mind-the-end-of-your-line/

2
  • Damn son, took all the way to this one. I had to commit after the first line but I was able to finally checkout master. That was not fun. Aug 9, 2017 at 3:02
  • 1
    I think this should be the accepted answer as it points out two most important causes. In my cause I am using MAC and its file system APFS. It is case preserving but not case sensitive
    – Stan
    Sep 21, 2023 at 23:42
29

If other answers are not working, trying adding the files, then resetting

$ git add -A
$ git reset --hard

In my case, this helped when there were a bunch of empty files that git was tracking.

2
  • 2
    This works. I just used $ git add . instead of $ git add -A Apr 28, 2020 at 13:10
  • This worked for me, too. I used $ git add . and then $ git reset --hard.
    – TazExprez
    Nov 6, 2021 at 1:02
9

Another cause for this might be case-insensitive file systems. If you have multiple folders in your repo on the same level whose names only differ by case, you will get hit by this. Browse the source repository using its web interface (e.g. GitHub or VSTS) to make sure.

For more information: https://stackoverflow.com/a/2016426/67824

3
  • To find the names of such files run git ls-tree -r --name-only HEAD | tr A-Z a-z | sort | uniq -d Sep 23, 2016 at 6:30
  • Apart from folders, this can also occur with multiple files differing only by case. I saw this with a Windows (non-case-sensitive) repo with origin on a Linux (case-sensitive) host - somehow we had got a copy of one file with differing case in there. Aug 21, 2020 at 4:00
  • exactly my issue !
    – phatnhse
    May 2, 2022 at 6:47
7

You can stash away your changes, then drop the stash:

git stash
git stash drop
1
  • 1
    You may also be interested in this.
    – Shahbaz
    Jul 8, 2012 at 12:27
6

Run clean command:

# Remove all untracked files and directories. (`-f` is `force`, `-d` is `remove directories`)

git clean -fd
2
  • 4
    Unfortunately this doesn't resolve issues related to line endings. Apr 10, 2017 at 13:06
  • This is only thing that resolve my issue, whatever my issue was. I had no .gitattributes file and line endings were not an issue since I'm on a linux box and all of the devs and our server are linux. Oct 22, 2018 at 19:25
5

None of these methods worked for me, the only solution was to nuke the whole repo and re-clone it. This includes stashing, resetting, adding then resetting, clrf settings, case sensitivity etc. Sigh..

1
  • Update, it turned out my mounted case sensitive filesystem wasn't actually case sensitive. After I fixed that this problem was fixable using the techniques mentioned above.
    – John Hunt
    Nov 7, 2016 at 14:26
5

I believe that there is an issue with git for Windows where git randomly writes the wrong line endings on checkout and the only workaround is to checkout some other branch and force git to ignore changes. Then checkout the branch you actually want to work on.

git checkout master -f
git checkout <your branch>

Be aware that this will discard any changes you might have intentionally made, so only do this if you have this problem immediately after checkout.

Edit: I might have actually got lucky the first time. It turns out that I got bit again after switching branches. It turned out that the files git was reporting as modified after changing branches was changing. (Apparently because git was not consistently applying the CRLF line ending to files properly.)

I updated to the latest git for Windows and hope the problem is gone.

0
4

Check your .gitattributes.

In my case I have *.js text eol=lf in it and git option core.autocrlf was true.

It brings me to the situation when git autoconverts my files line endings and prevent me to fix it and even git reset --hard HEAD didn`t do anything.

I fix it with commenting *.js text eol=lf in my .gitattributes and uncommented it after it.

Looks like it just git magic.

2
  • 1
    This was it for me, upgrading my project introduced *.bat text eol=crlf to my project.
    – Leo
    Apr 20, 2020 at 19:54
  • I had the same problem with gradlew.bat, does this mean eol=crlf doesn't work on Windows?
    – TWiStErRob
    Jan 19, 2023 at 17:56
1

I had the same problem. I did git reset --hard HEAD but still every time I did git status I was seeing some files as modified.

My solution was relatively simple. I just closed my IDE (here it was Xcode) and close my command line (here it was terminal on my Mac OS) and tried it again and it worked .

Yet I was never able to find what originated the problem.

1
  • In my case it was the IDE (codium) which always updated the file .classpath which then appeared again and again as modified.
    – dr0i
    Dec 15, 2020 at 13:51
1

I hit a similar issue also involving .gitattributes, but for my case involved GitHub's LFS. While this isn't exactly the OP's scenario, I think it provides an opportunity to illustrate what the .gitattributes file does and why it can lead to unstaged changes in the form of "phantom" differences.

In my case, a file already was in my repository, like since the beginning of time. In a recent commit, I added a new git-lfs track rule using a pattern that was in hindsight a bit too broad, and ending up matching this ancient file. I know I didn't want to change this file; I know I didn't change the file, but now it was in my unstaged changes and no amount of checkouts or hard resets on that file was going to fix it. Why?

GitHub LFS extension works primarily through leveraging the hooks that git provides access through via the .gitattributes file. Generally, entries in .gitattributes specify how matching files should be processed. In the OP's case, it was concerned with normalizing line endings; in mine, it was whether to let LFS handle the file storage. In either case, the actually file that git sees when computing a git diff does not match the file that you see when you inspect the file. Hence, if you change how a file is processed via the .gitattributes pattern that matches it, it will show up as unstaged changes in the status report, even if there really is no change in the file.

All that said, my "answer" to the question is that if the change in .gitattributes file is something you wanted to do, you should should really just add the changes and move on. If not, then amend the .gitattributes to better represent what you want to be doing.

References

  1. GitHub LFS Specification - Great description of how they hook into the clean and smudge function calls to replace your file in the git objects with a simple text file with a hash.

  2. gitattributes Documentation - All the details on what is available to customize how git processes your documents.

1

try to simply git restore .

That's what git says & it's working

0

Similar issue, although I'm sure only on surface. Anyway, it may help someone: what I did (FWIW, in SourceTree): stashed the uncommitted file, then did a hard reset.

0

As other answers have pointed out, the problem is because of line-end auto correcting. I encountered this issue with *.svg files. I used svg file for icons in my project.

To solve this issue, I let git know that svg files should be treated as binary instead of text by adding the below line to .gitattributes

*.svg binary

0

In a similar situation. As a result of many years of torment with many programmers who were able to stuff different encodings of the ends of lines (.asp, .js, .css ... not the essence) into one file. Some time ago refused .gitattributes. Settings for repo left autorclf = true andsafecrlf = warn.

The last problem arose when synchronizing from the archive with different ends of lines. After copying from the archive, in the git status many files are changed with the note

The line will have its original line endings in your working directory. warning: LF will be replaced by CRLF in ...

Tips from How to normalize working tree line endings in Git? helped

git add -u

0

Another possibility that I encountered was that one of the packages in the repository ended up with a detached HEAD. If none of the answer here helps and you encounter a git status message like this you might have the same problem:

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   path/to/package (new commits)

Going into the path/to/package folder a git status should yield the following result:

HEAD detached at 7515f05

And the following command should then fix the issue, where master should be replaced by your local branch:

git checkout master

And you'll get a message that your local branch will be some amounts of commits behind the remote branch. git pull and you should be out of the woods!

0

Enviro: Visual Studio 2019 + git + Azure DevOps.

Behavior

Ran into this situation after attempting a hard reset, but mine was not due to line endings.

git was hanging when executing a number of commands; in other words, it would never get back to the command prompt. It would show files in a given command as having been processed, but it would not complete all "phases" of a command.

git clone, as an example:

enter image description here

Solution

Create a new Personal Access Token PAT

Was a security/credential issue related to my existing PAT, but I could not find any logging to tell me that it was failing.

-3

For some reason

git add .

didn't work, but

$ git add -A

worked out!

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.