416

i'm adding this to .gitignore file

.idea/*

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
Ufuk Hacıoğulları
  • 37,978
  • 12
  • 114
  • 156
Said Kaldybaev
  • 9,380
  • 8
  • 36
  • 53
  • 4
    possible duplicate of [Making git "forget" about a file that was tracked but is now in .gitignore](http://stackoverflow.com/questions/1274057/making-git-forget-about-a-file-that-was-tracked-but-is-now-in-gitignore) – AbdelHady Jul 04 '15 at 18:38

13 Answers13

647

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.
mcls
  • 9,071
  • 2
  • 29
  • 28
  • Would you please explain what you meant by "Just adding .idea/ would work too". Is this an alternative solution to using `.gitignore` and then doing `git rm -cached` ? – Mehrad Apr 23 '15 at 01:43
  • 3
    @Mehrad no need for the * in .gitignore – Steve Pitchers Apr 24 '15 at 14:38
  • 2
    No, just adding .idea/ doesn't work, because like you said in the answer, it's already in the index. – Henrique de Sousa Jun 27 '15 at 21:14
  • 28
    So you're saying that you need to `commit` after removing the cached files? That really seems counter-intuitive; these are files I do NOT want to commit. And I do not want them deleted from the repository (I just want them to no longer be hanging around cluttering my `git status`). Or am I just really confused? – SMBiggs Jul 09 '16 at 03:47
  • 13
    His files were already tracked by git because of an earlier commit, so in order to remove them, you have to create a new commit that removes them from the repository. – mcls Jul 19 '16 at 05:47
  • @ScottBiggs check out http://stackoverflow.com/questions/1753070/git-ignore-files-only-locally for info on ignoring that directory locally (without pushing any changes) – lmsurprenant Oct 31 '16 at 14:37
  • It removes the file from the index, but exposes the file content to anyone who would bother to `git diff ` :-( https://www.evernote.com/l/ACjPnONyq-lFF6OZII8MQ5uLQ5TaLuplyWs – pilat Apr 11 '18 at 08:06
  • 3
    `git rm -r --cached *` I used this command to remove all those all kind of files which are cached...but it removed mine all file... please help me to undo this action! @maartencls – iamabhaykmr Jul 11 '19 at 11:45
  • What to do when you wanna do this for all files in a folder? – mesqueeb Dec 27 '21 at 02:52
  • @iamabhaykmr You can use `git reset --mixed` to restore the index, or `git reset --hard` to restore everything. In the future though, avoid running commands you don't fully understand. `--cached` isn't for removing some temporary files. In Git, `cache` is another term for the staging index, so you're removing these files from the index (while still keeping them in the working tree). If you run it with `*` it will surely remove everything. – ADTC Nov 23 '22 at 05:20
137

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.
Serge Stroobandt
  • 28,495
  • 9
  • 107
  • 102
Sidhanshu_
  • 1,714
  • 1
  • 7
  • 10
  • 2
    This was useful for me, I would just note that in windows cmd the single quotes don't seem to work in #3. Using git commit -am "Removing ignored files" Does work, though. – Sundance.101 Jul 18 '18 at 08:41
  • 1
    No worries, and thanks again for the clear and concise answer. – Sundance.101 Jul 26 '18 at 11:20
  • 14
    This solution is messing with my project files really. I had to revert everything after doing this. – Fatmajk Aug 02 '19 at 09:08
  • Wow.. this is worked for me. I was searching since long back for this. thank you.. – Vijay Sep 02 '22 at 10:59
  • 1
    you don't need to remove all you can remove only the file you don't want to include in ignore git rm -r --cached file_name .. rest steps are fine – Pawan Deore Sep 22 '22 at 03:59
23

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.

RRE Designs
  • 237
  • 2
  • 3
  • for me 'git update-index --skip-worktree path/to/file' was more appropriate, since I wanted to keep modifying the local file – klsx Dec 28 '21 at 17:28
  • You can do what I suggested and still edit the file as you please. Git won't check deltas on that file and that's pretty much all you need, to ignore that file. – RRE Designs Dec 29 '21 at 23:06
  • 2
    Glad to finally find a solution that works on modern Git (in 2022). `git update-index --assume-unchanged .mydir/myfile` worked perfectly for a "stuck" file that wasn't being ignored. – James Jul 27 '22 at 05:12
  • 1
    _"but it also deletes the file from the remote. I have no idea why that is."_ That is actually the purpose of `git rm --cached` :) It removes files from the repo (including remote repo) and stops tracking them if they're in `.gitignore`. And yes, you're right. If you just want to stop tracking locally without affecting remote, you need to use `git update-index --skip-worktree` (note that it shouldn't be `--assume-unchanged`). – ADTC Nov 23 '22 at 05:24
7

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.

Serge Stroobandt
  • 28,495
  • 9
  • 107
  • 102
6

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.

kiloton
  • 105
  • 2
  • 10
  • Thanks! Yeah I had this issue after switching between branches that had different .gitignores - The Github desktop client decided to stage files for commit – jameslol Aug 07 '20 at 23:44
  • 1
    Be sure to have a backup especially of nontracked files like user-uploads, u can easily loose them if you are not 100 percent sure what u are doing – john Smith Oct 23 '21 at 14:14
4

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.

Abhilash Ramteke
  • 367
  • 1
  • 4
  • 11
4

This will allow you to 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 'Remove ignored files'

These actions accomplish the following:

Step 1: This command will clear the index of all cached files.

Step 2: This command will add every file besides those that are on the gitignore list.

Step 3: By using this command, you can remove the files you wish git to ignore while keeping them in your local directory and recommit your files.

Babu K
  • 41
  • 2
2

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

rahul
  • 208
  • 1
  • 7
2

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.

Abraham
  • 12,140
  • 4
  • 56
  • 92
0

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.

gismatthew
  • 96
  • 1
  • 3
0

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

Terrence Brannon
  • 4,760
  • 7
  • 42
  • 61
0

Warning. The files, however, will be there in all previous commits. Therefore the .git/ will have the information. If the file to be removes is heavy or sensitive (like passwords) it can be a problem. To compleately remove it from history do the following:

  1. First the accepted answer:
git rm -r --cached .idea/*
git add .idea/
3. git commit -m 'Removing ignored idea folder'
  1. Clean history.

Do it in a clean clone is recommended. So save the folder as backup and clone it again.

git clone <your repo>
cd <your repo>

Just check the remote is configured correctly using for example git push.

Now the real stuff

pip install git-filter-repo

git filter-repo --path .idea/ --invert-paths
J Agustin Barrachina
  • 3,501
  • 1
  • 32
  • 52
-2
  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

Community
  • 1
  • 1
Lim Kean Phang
  • 501
  • 4
  • 6