git still shows files as modified after adding to .gitignore


Git Problem Overview

i'm adding this to .gitignore file


but anyway the status is:

#       modified:   .gitignore
#       modified:   .idea/.generators
#       modified:   .idea/dovezu.iml
#       modified:   .idea/misc.xml
#       modified:   .idea/workspace.xml

what am i doing wrong ? i even added .idea/* to the global ~/.gitignore_global but git status, anyway shows me:

#       modified:   .gitignore
#       modified:   .idea/.generators
#       modified:   .idea/dovezu.iml
#       modified:   .idea/misc.xml
#       modified:   .idea/workspace.xml

Git Solutions

Solution 1 - Git

Your .gitignore is working, but it still tracks the files because they were already in the index.

To stop this you have to do : git rm -r --cached .idea/

When you commit the .idea/ directory will be removed from your git repository and the following commits will ignore the .idea/ directory.

PS: You could use .idea/ instead of .idea/* to ignore a directory. You can find more info about the patterns on the .gitignore man page.

Helpful quote from the git-rm man page

    Use this option to unstage and remove paths only from the index. 
    Working tree files, whether modified or not, will be left alone.

Solution 2 - Git

To the people who might be searching for this issue still, are looking at this page only.

This will help you remove cached index files, and then only add the ones you need, including changes to your .gitignore file.

1. git rm -r --cached .
2. git add .
3. git commit -m 'Removing ignored files'

Here is a little bit more info.

> 1. This command will remove all cached files from index. > 2. This command will add all files except those which are mentioned in gitignore.
> 3. This command will commit your files again and remove the files you want git to ignore, but keep them in your local directory.

Solution 3 - Git

Using git rm --cached *file* is not working fine for me (I'm aware this question is 8 years old, but it still shows at the top of the search for this topic), it does remove the file from the index, but it also deletes the file from the remote.

I have no idea why that is. All I wanted was keeping my local config isolated (otherwise I had to comment the localhost base url before every commit), not delete the remote equivalent to config.

Reading some more I found what seems to be the proper way to do this, and the only way that did what I needed, although it does require more attention, especially during merges.

Anyway, all it requires is git update-index --assume-unchanged *path/to/file*.

As far as I understand, this is the most notable thing to keep in mind:

> Git will fail (gracefully) in case it needs to modify this file in the index e.g. when merging in a commit; thus, in case the assumed-untracked file is changed upstream, you will need to handle the situation manually.

Solution 4 - Git

The solutions offered here and in other places didn't work for me, so I'll add to the discussion for future readers. I admittedly don't fully understand the procedure yet, but have finally solved my (similar) problem and want to share.

I had accidentally cached some doc-directories with several hundred files when working with git in IntelliJ IDEA on Windows 10, and after adding them to .gitignore (and PROBABLY moving them around a bit) I couldn't get them removed from the Default Changelist.

I first commited the actual changes I had made, then went about solving this - took me far too long. I tried git rm -r --cached . but would always get path-spec ERRORS, with different variations of the path-spec as well as with the -f and -r flags.

git status would still show the filenames, so I tried using some of those verbatim with git rm -cached, but no luck. Stashing and unstashing the changes seemed to work, but they got queued again after a time (I'm a bity hazy on the exact timeframe). I have finally removed these entries for good using

git reset

I assume this is only a GOOD IDEA when you have no changes staged/cached that you actually want to commit.

Solution 5 - Git

Simply add git rm -r --cached <folder_name/file_name>

Sometimes, you update the .gitignore file after the commit command of files. So, the files get cached in the memory. To remove the cached files, use the above command.

Solution 6 - Git

if you have .idea/* already added in your .gitignore and if git rm -r --cached .idea/ command does not work (note: shows error-> fatal: pathspec '.idea/' did not match any files) try this

remove .idea file from your app run this command

rm -rf .idea

run git status now and check while running the app .idea folder will be created again but it will not be tracked

Solution 7 - Git

This also happens If you have saved it with something like echo node_modules >> .gitignore

The windows terminal saves the file in UCS-2 LE BOM encoding and git doesn't seem to accept that.

So, you can open the file with Notepad and saved with UTF-8 encoding

notepad save utf-8 encoding

It Works now.

I think they should fix this since doing echo "filetoignore" >> .gitignore actually seems very handy.

Solution 8 - Git

In my case, the .gitignore file is somehow corrupted. It sometimes shows up OK but sometimes is gibberish. I guess the encoding might be to blame but cannot be sure. To solve the issue, I deleted the file (previously created through echo > .gitgore in VS Code commandline) and created a new one manually in the file system (Explorer), then added my ignore rules. After the new .ignore was in place, everything works fine.

Solution 9 - Git

I will chime in here. In my case, a subdirectory had a git repo within it because I unpacked a zip file from someone else and then recursively copied everything in their unpacked zip into my repo without inspecting what I was copying.

Once I blew away the .git directory inside my existing repo, all was well..

Solution 10 - Git

update-index --skip-worktree

The specific git option to ignore configuration files is:

$ git update-index --skip-worktree <filepath>

Note that there is no need to add something to .gitignore.

To cancel this, for example when the generic structure of a configuration file has changed:

$ git update-index --no-skip-worktree <filepath>

--skip-worktree is used to instruct git not to index a file change to a specific file ever. This might be because developers usually need to change the file locally in order to test the project. For example, if the main repository upstream hosts a production-ready configuration file and accidental commit changes to this file should be prevented.

Whereas --assume-unchanged assumes that a developer should never change that file. This flag is meant only for improving the performance of git with respect to never changing folders like software development kit (SDK) components.

Solution 11 - Git

  1. Git add .

  2. Git status //Check file that being modified

    // git reset HEAD --- replace to which file you want to ignore

  3. git reset HEAD .idea/ <-- Those who wanted to exclude .idea from before commit // git check status and the idea file will be gone, and you're ready to go!

  4. git commit -m ''

  5. git push


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSaid KaldybaevView Question on Stackoverflow
Solution 1 - GitmclsView Answer on Stackoverflow
Solution 2 - GitSidhanshu_View Answer on Stackoverflow
Solution 3 - GitRRE DesignsView Answer on Stackoverflow
Solution 4 - GitkilotonView Answer on Stackoverflow
Solution 5 - GitAbhilash RamtekeView Answer on Stackoverflow
Solution 6 - GitrahulView Answer on Stackoverflow
Solution 7 - GitAbrahamView Answer on Stackoverflow
Solution 8 - GitgismatthewView Answer on Stackoverflow
Solution 9 - GitTerrence BrannonView Answer on Stackoverflow
Solution 10 - GitSerge StroobandtView Answer on Stackoverflow
Solution 11 - GitLim Kean PhangView Answer on Stackoverflow