我通过以下方式解决了我的问题:
首先,我是 Windows 用户,但我也遇到过类似的问题。所以,我在这里发布我的解决方案。
有时 .gitignore 不能正常工作的原因很简单。这是由于 EOL 转换行为所致。
这里有一个快速解决方法
编辑 > EOL 转换 > Windows 格式 > 保存
您可以将此归咎于您的文本编辑器设置。
例如:
由于我是一名 Windows 开发人员,我通常使用 Notepad++ 来编辑我的文本,这与 Vim 用户不同。
所以发生的情况是,当我使用 Notepad++ 打开我的 .gitignore 文件时,它看起来像这样:
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
# See https://help.github.com/ignore-files/ for more about ignoring files.
# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates
*.dll
*.force
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
如果我使用默认记事本打开同一个文件,这就是我得到的
## Ignore Visual Studio temporary files, build results, and ## files generated by popular Visual Studio add-ons. ## ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore # See https://help.github.com/ignore-files/ for more about ignoring files. # User-specific files *.suo *.user *.userosscache
所以,您可能已经通过查看输出猜到了。 .gitignore 中的所有内容都变成了一个衬里,并且由于开头有一个##,所以它就像所有内容都被注释了一样。
解决这个问题的方法很简单:
只需使用 Notepad++ 打开您的 .gitignore 文件,然后执行以下操作
编辑 > EOL 转换 > Windows 格式 > 保存
下次您使用 Windows 默认记事本打开同一个文件时,所有内容都应正确格式化。尝试一下,看看这是否适合您。