Git似乎忽略了我的.gitignore文件-.gitignorefile是否已损坏?Git希望使用哪种文件格式、区域设置或文化?

我的.gitignore:

# This is a comment
debug.log
nbproject/

git状态的输出:

# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       debug.log
#       nbproject/
nothing added to commit but untracked files present (use "git add" to track)

我希望debug.log和nbproject/不出现在未跟踪的文件列表中。

我应该从哪里开始解决这个问题?


当前回答

在我的情况下,这是因为文件已经存在于存储库中,我试图忽略它。

以下是我为解决问题所做的工作:

将文件复制到临时文件夹从我的项目文件夹中删除它们。提交将这些文件从存储库中删除的更改将这些文件重新添加到我的项目文件夹

到那时,我对这些文件所做的任何更改都被忽略了。

我认为您不能忽略存储库中已经存在的文件。

其他回答

即使到目前为止你还没有跟踪这些文件,Git似乎也可以在你将它们添加到.gitignore之后“知道”它们。

警告:首先提交或隐藏当前更改,否则您将丢失它们。

然后从Git存储库的顶部文件夹运行以下命令:

git rm -r --cached .
git add .
git commit -m "fixed untracked files"

每当我遇到git正在跟踪.gitignore中列出的文件的情况时,我会使用以下方法:

git update-index --skip-worktree <file_name>

还可以查看放置.gitignore的目录。

它应该位于项目的根目录中:

./myproject/.gitignore

不在

./myproject/.git/.gitignore

和其他解决方案一样,首先提交,并注意您将丢失任何未提交的更改。

我有更好的结果:

git rm -r --cached .
git reset HEAD --hard
git status

请注意,状态现在不应该有任何修改过的文件。

这里其他答案没有涉及的一个棘手问题是,如果您有内联注释,.gitignore文件将无法工作,如下所示:

foo/bar # The bar file contains sensitive data so we don't want to make this public

因此,如果您确实有这样的评论,请这样更改它们:

# The bar file contains sensitive data so we don't want to make this public
foo/bar