git still shows files as modified after adding to

2019-01-20 21:59发布

问题:

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:

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.


回答2:

To the people who might be searching for this issue still, are looking at this page only.

Do this and voila, it will be sorted.

  1. git rm -r --cached .
  2. git add -A
  3. git commit -m 'Removing ignored files'

source

first command will remove all files from index

second command will add all files except those which are mentioned in gitignore

last command will commit your files again and remove the files you want git to ignore, but keep them in your local directory.

Note : use "Removing ignored files" in 3rd step in windows.



回答3:

  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


标签: git gitignore