添加到 .gitignore 后,git 仍将文件显示为已修改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9750606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
git still shows files as modified after adding to .gitignore
提问by Said Kaldybaev
i'm adding this to .gitignorefile
我将此添加到.gitignore文件
.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_globalbut git status, anyway shows me:
我究竟做错了什么 ?我什至将 .idea/* 添加到全局~/.gitignore_global但 git status,无论如何显示给我:
# modified: .gitignore
# modified: .idea/.generators
# modified: .idea/dovezu.iml
# modified: .idea/misc.xml
# modified: .idea/workspace.xml
回答by mcls
Your .gitignore
is working, but it still tracks the files because they were already in the index.
您.gitignore
正在工作,但它仍然跟踪文件,因为它们已经在索引中。
To stop this you have to do : git rm -r --cached .idea/
要阻止这种情况,您必须执行以下操作: 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.
当您提交该.idea/
目录时,该目录将从您的 git 存储库中删除,并且以下提交将忽略该.idea/
目录。
PS: You could use .idea/
instead of .idea/*
to ignore a directory. You can find more info about the patterns on the .gitignoreman page.
PS:您可以使用.idea/
而不是.idea/*
忽略目录。您可以在.gitignore手册页上找到有关模式的更多信息。
Helpful quote from the git-rm
man page
来自git-rm
手册页的有用引用
--cached
Use this option to unstage and remove paths only from the index.
Working tree files, whether modified or not, will be left alone.
回答by Sidhanshu_
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.
这将帮助您删除缓存的索引文件,然后只添加您需要的.gitignore
文件,包括对文件的更改。
1. git rm -r --cached .
2. git add .
3. git commit -m 'Removing ignored files'
Hereis a little bit more info.
这里有更多信息。
- This command will remove all cached files from index.
- This command will add all files except those which are mentioned in
gitignore
.- This command will commit your files again and remove the files you want git to ignore, but keep them in your local directory.
- 此命令将从索引中删除所有缓存文件。
- 此命令将添加除
gitignore
.- 此命令将再次提交您的文件并删除您希望 git 忽略的文件,但将它们保留在您的本地目录中。
回答by Abhilash Ramteke
Simply add git rm -r --cached <folder_name/file_name>
只需添加 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.
有时,您会在文件的提交命令之后更新 .gitignore 文件。因此,文件被缓存在内存中。要删除缓存文件,请使用上述命令。
回答by Lim Kean Phang
- Git add .
Git status //Check file that being modified
// git reset HEAD --- replace to which file you want to ignore
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!
git commit -m ''
- git push
- git 添加 .
git status //查看被修改的文件
// git reset HEAD --- 替换为要忽略的文件
git reset HEAD .idea/ <-- 那些想在提交之前排除 .idea 的人 // git check status 并且idea 文件将消失,你准备好了!
git commit -m ''
- 推