Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git still shows files as modified after adding to .gitignore

Tags:

git

gitignore

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 
like image 988
Said Kaldybaev Avatar asked Mar 17 '12 14:03

Said Kaldybaev


People also ask

Why git ignore is not ignoring files?

gitignore only ignores files that are not part of the repository yet. If you already git add ed some files, their changes will still be tracked. To remove those files from your repository (but not from your file system) use git rm --cached on them.

How do I force add an ignored file in git?

The git add command can be used to add ignored files with the -f (force) option.

Does .gitignore need to be tracked?

Yes, you can track the . gitignore file, but you do not have to. The main reason of having this file into repository is to have everyone working on the project, ignoring same files and folders. Also see this: Should you commit .


2 Answers

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. 
like image 169
mcls Avatar answered Oct 06 '22 11:10

mcls


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.
like image 22
Sidhanshu_ Avatar answered Oct 06 '22 12:10

Sidhanshu_