.gitignore被Git忽略

本文翻译自:.gitignore is ignored by Git

My .gitignore file seems to be being ignored by Git - could the .gitignore file be corrupt? 我的.gitignore文件似乎已被Git忽略.gitignore文件是否已损坏? Which file format, locale or culture does Git expect? Git需要哪种文件格式,语言环境或文化?

My .gitignore : 我的.gitignore

# This is a comment
debug.log
nbproject/

Output from git status : git status输出:

# 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)

I would like debug.log and nbproject/ not to appear in the untracked files list. 我希望debug.lognbproject/不会出现在未跟踪的文件列表中。

Where should I start looking to fix this? 我应该从哪里着手解决此问题?


#1楼

参考:https://stackoom.com/question/m34B/gitignore被Git忽略


#2楼

Even if you haven't tracked the files so far, Git seems to be able to "know" about them even after you add them to .gitignore . 即使到目前为止您尚未跟踪文件,即使将它们添加到.gitignore ,Git似乎也能够“知道”它们。

NOTE: First commit your current changes, or you will lose them. 注意:首先提交您当前的更改,否则您将丢失它们。

Then run the following commands from the top folder of your Git repository: 然后从Git存储库的顶部文件夹中运行以下命令:

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

#3楼

Fixed. 固定。 OK, I created the .gitignore file in Notepad on Windows and it wasn't working. 好的,我在Windows的记事本中创建了.gitignore文件,但该文件无法正常工作。 When I viewed the .gitignore file on Linux it looked like organised gibberish - perhaps Notepad had written out Unicode rather than ASCII or whatever 8-bit is. 当我在Linux上查看.gitignore文件时,它看起来像是乱七八糟的东西-也许记事本写出了Unicode而不是ASCII或任何8位。

So I rewrote the file on my Linux box, and when I pulled it back into Windows it works fine! 因此,我将文件重写了Linux上的文件,当我将其拉回到Windows时,它运行良好! Hurrah! 欢呼!


#4楼

One thing to also look at: Are you saving your .gitignore file with the correct line endings? 还需要注意的一件事:您是否在保存带有正确行尾的.gitignore文件?

Windows: 视窗:

If you're using it on Windows, are you saving it with Windows line endings? 如果在Windows上使用它,是否将其与Windows行尾一起保存? Not all programs will do this by default; 并非所有程序默认都会执行此操作; Notepad++ and many PHP editors default to Linux line endings so the files will be server compatible. Notepad ++和许多PHP编辑器默认使用Linux行尾,因此文件将与服务器兼容。 One easy way to check this, is open the file in Windows Notepad. 一种简单的检查方法是在Windows记事本中打开文件。 If everything appears on one line, then the file was saved with Linux line endings. 如果所有内容都显示在一行上,则该文件以Linux行尾保存。

Linux: Linux:

If you are having trouble with the file working in a Linux environment, open the file in an editor such as Emacs or nano . 如果您在Linux环境中无法正常使用文件,请在诸如Emacs或nano的编辑器中打开文件。 If you see any non-printable characters, then the file was saved with Windows line endings. 如果看到任何不可打印的字符,则该文件以Windows行尾保存。


#5楼

Another cause of this issue is blank spaces or tabs before the statement: 导致此问题的另一个原因是语句前的空格或制表符:

Example: 例:

# Be aware of the following:
 notWorkingIgnore.*
workingIgnore.*

And as pointed out by the comment below a trailing space can be an issue as well: 正如下面的评论所指出的,尾随空格也可能是一个问题:

# Be aware of the following:
notWorkingIgnore.* #<-Space
workingIgnore.*#<-Nospace

#6楼

Without adding another commit to your project, one line will be enough to make .gitignore work as it is supposed to: 无需在项目中添加其他提交,只需一行就可以使.gitignore正常工作:

git rm -r --cached debug.log nbproject

This will remove them from the repository, but still keep them physically. 这会将它们从存储库中删除,但仍然保持物理状态。 In plain English, it deletes any history of changes related to them, and also will not track their change in any future commit. 用简单的英语,它将删除与它们相关的任何更改历史记录,并且在以后的任何提交中都不会跟踪它们的更改。 You may find a better explanation here . 您可能会在这里找到更好的解释。

发布了0 篇原创文章 · 获赞 136 · 访问量 83万+

猜你喜欢

转载自blog.csdn.net/xfxf996/article/details/105238025