git still shows files as modified after adding to .gitignore
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
--cached
Use this option to unstage and remove paths only from the index.
Working tree files, whether modified or not, will be left alone.
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.
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.
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.
- This command will remove all cached files from index.
- This command will add all files except those which are mentioned in
gitignore
.- This command will commit your files again and remove the files you want git to ignore, but keep them in your local directory.