59

Working on a Symfony2 project using PhpStorm IDE, I have added couple of files to git ignore but one file despite being added to git ignore is always appearing. I have tried multiple times but its always there

.gitignore file:

/app/config/parameters.yml
/bin/
/build/
/composer.phar
/vendor/
/web/bundles/
/app/bootstrap.php.cache
/app/cache/*
!app/cache/.gitkeep
/app/config/parameters.yml
/app/logs/*
!app/logs/.gitkeep
/app/phpunit.xml

#IDE metadata
**.idea/**

#Bash files
run.sh

And it is the .idea folder/files that Git just will not ignore:

On branch develop
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:   .idea/workspace.xml

no changes added to commit (use "git add" and/or "git commit -a")

Any idea why git will just not ignore the whole directory like i have specified in gitignore?

Vampire
  • 35,631
  • 4
  • 76
  • 102
John
  • 1,595
  • 4
  • 21
  • 44
  • Possible duplicate of [Ignore files that have already been committed to a Git repository](http://stackoverflow.com/questions/1139762/ignore-files-that-have-already-been-committed-to-a-git-repository) – 1615903 Apr 26 '16 at 08:22

5 Answers5

74

Git never ignores changes to tracked files. As it appears as modified, the file is under version control (the idea/workspace.xml file usually should not be) and thus changes to it are tracked. Delete it from the index, leaving your local copy intact with git rm --cached .idea/workspace.xml and commit this change. From then on it will be ignored unless you force-add it back to the repository or change your gitignore settings.

Vampire
  • 35,631
  • 4
  • 76
  • 102
  • Ok let me see if I understand this, So once you commit a file and then decide to add this file to gitignore, git will track changes to this file anyway as it exists in git cache crated during the first commit including that file...? If I understand this right is there a way to view git cache content...? – John Apr 25 '16 at 14:07
  • You don't even have to commit. As soon as you add a file to the index it is tracked, no matter what is written in any gitignore file. About your other question. "Git Cache" is not an official term, so I'm not sure what you are referring to exactly. What do you want to know? – Vampire Apr 25 '16 at 14:12
  • Ok i c thx, I would like to see a list of all the files that are being tracked by git – John Apr 25 '16 at 14:15
  • `git ls-tree -r --full-tree --name-only ` – Vampire Apr 25 '16 at 14:21
  • But you can also use `ls-files` depends on what exactly you want to see. With `ls-files` you can list the files in the index and in the worktree and how they related. with `ls-tree` you list the tree of e. g. how it is at time of a specific commit. – Vampire Apr 25 '16 at 14:23
  • Ok all this makes sense and works thank you so much for your time much appreciated regards – John Apr 25 '16 at 14:25
  • In my case, I have 0 commits and nothing staged and it still won't ignore it – Joe Phillips Jan 28 '21 at 22:35
  • Well, do you have a `.gitignore` which defines it should be ignored? Git does not ignore it without being told so. :-) – Vampire Apr 05 '23 at 15:28
34

Once you commit the file it will begin to be tracked.
In order to remove the file from this point you have to remove them from the repository.

What you need to do now is to delete the entire .idea folder, commit the deletion, add the idea extension to your .gitignore file.

Note

You can still ignore added files with the assume-unchanged flag

--[no-]assume-unchanged

When this flag is specified, the object names recorded for the paths are not updated.
Instead, this option sets/unsets the "assume unchanged" bit for the paths.

When the assume unchanged bit is on, the user promises not to change the file and allows Git to assume that the working tree file matches what is recorded in the index.

If you want to change the working tree file, you need to unset the bit to tell Git.

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.


Explaining how to do from command line, can be done via IDEA as well.

# Remove the file from the repository
git rm --cached .idea/

# now update your gitignore file to ignore this folder
echo '.idea' >> .gitignore

# add the .gitignore file
git add .gitignore

git commit -m "Removed .idea files"
git push origin <branch>
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • Never ever use the --assume-unchanged flag, except you are a real Git crack and know exactly what you do. Otherwise you just risk your files. Its usage is constantly discouraged in #git in IRC and not without reason – Vampire Apr 25 '16 at 12:19
  • The main problem is that you can forget to turn it "off" and lose changes. – CodeWizard Apr 25 '16 at 12:20
  • 1
    Well, the average user will forget that this switch exists 5 minutes after he had used it. ;-) – Vampire Apr 25 '16 at 12:57
  • 2
    git rm --cached -r .idea/ now – Wajdy Essam Feb 24 '18 at 19:11
  • 3
    running `git rm --cached -r .\.idea\ ` or `git rm --cached -r .idea/` gives me the message "fatal: pathspec '.\.idea\' did not match any files", with or without the -r flag in powershell – Josh Desmond Oct 29 '19 at 23:11
  • `git rm --cached -r .` to clear all cache. But you need to run it on a clean status. – s.alem Feb 18 '22 at 12:16
8
#idea folder
.idea/

This worked fine for me

Faheem
  • 930
  • 10
  • 7
1

I added this in the gitignore file and worked for me

**/.idea

1

When we commit something git started to tracking the folder/project. Unnecessary file we can also delete from the project.

To delete .idea/ folder from the project. Follow these steps as shwon below:

  1. Make sure that, you have added .idea/ to your .gitignore file.
  2. Run these following command

    git rm -r --cached .
    git add .
    git commit -m "untracked fixed"

Hopefully, it will fix the problem.