由于某种原因,我的.gitignore文件无法正常工作,而且没有多少谷歌搜索能够修复它

*.apk
*.ap_
*.dex
*.class
**/bin/
**/gen/
.gradle/
build/
local.properties
**/proguard/
*.log

它在主目录中,这是我的git repo。我运行Git 1.8.4.2,因为我使用的是运行OSX 10.8.6的MacBook。


当前回答

我通过以下方式解决了问题:

首先,我是一个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默认记事本打开同一文件时,所有内容都应正确格式化。试试看这是否适合你。

其他回答

@Ahmad的答案是有效的,但如果你只是想忽略一个特定文件或几个文件,请按照@Nicolas的建议做

步骤1

将文件名添加到.gitignore文件

步骤2

[从git缓存中删除文件名(文件路径)

git rm—缓存的文件名

步骤3

提交更改git添加文件名

gitcommit-m“将文件名添加到.gitignore”

它将保持您的git历史记录干净,因为如果您执行git rm-r--cached。并将所有文件加回去提交,这将污染您的git历史记录(这将显示您在一次提交时添加了大量文件)。我不确定我表达的想法是否正确,但希望您明白这一点

我通过以下方式解决了问题:

首先,我是一个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默认记事本打开同一文件时,所有内容都应正确格式化。试试看这是否适合你。

此外,评论必须在自己的行上。它们不能放在入口之后。所以这行不通:

/node_modules  # DON'T COMMENT HERE (since nullifies entire line)

但这将奏效:

# fine to comment here
/node_modules

git重置了吗?对任何人来说都很难?我并不是说这是一个好的解决方案,只是我第一次尝试的时候似乎奏效了。

我知道这已经有很多答案了,但我最后回答这个问题的方式如下。。。

在新的repo中安装npm之前,我已经完成了'node_modules'>.gitignore,直到我更改了.gitignore'(添加了'**/node_modules'、'/node_mdules'等),然后进入git状态,我才意识到它是二进制格式,而不是文本格式,因此git无法识别它。

我通过删除.gitignore文件并将其重新添加到编辑器中来解决问题。