419

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
1

13 Answers 13

653

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.
10
  • 3
    @Mehrad no need for the * in .gitignore Apr 24, 2015 at 14:38
  • 2
    No, just adding .idea/ doesn't work, because like you said in the answer, it's already in the index. Jun 27, 2015 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 9, 2016 at 3: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, 2016 at 5:47
  • 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 Jul 11, 2019 at 11:45
138

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.
5
  • 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. Jul 18, 2018 at 8:41
  • 1
    No worries, and thanks again for the clear and concise answer. Jul 26, 2018 at 11:20
  • 15
    This solution is messing with my project files really. I had to revert everything after doing this.
    – Fatmajk
    Aug 2, 2019 at 9:08
  • Wow.. this is worked for me. I was searching since long back for this. thank you..
    – Vijay
    Sep 2, 2022 at 10:59
  • 2
    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 Sep 22, 2022 at 3:59
25

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.

4
  • 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, 2021 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. Dec 29, 2021 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, 2022 at 5: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, 2022 at 5:24
9

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.

1
  • Thanks, this addressed my use case. (I wanted to stub in the credential file format without committing any real credentials to the repo.) I just hope I can remember how to undo it when the time comes! If you get a "fatal: Unable to mark file" error, see this question on case sensitivity on Windows. Similarly, under Windows it is necessary to use "/" instead of "\" as a path delimiter. Sep 21 at 15:45
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.

2
  • 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 7, 2020 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, 2021 at 14:14
5

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.

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.

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

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.

1
  • After SO much searching, this finally fixed my problem. Thank you!
    – HFBrowning
    Sep 3, 2021 at 18:25
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.

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

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

1
  • This would be an assumption that the OP wants to reset. May 16, 2019 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.