Git没有忽略一个新的未跟踪文件,尽管它在.gitignore中。

huangapple go评论63阅读模式
英文:

Git not ignoring an new untracked file even though it is in .gitignore

问题

.gitignore 文件似乎没有按预期工作。您可以尝试以下步骤来解决问题:

  1. 确保 .gitignore 文件位于您的仓库根目录中。

  2. 检查 .gitignore 文件的内容,确保您正确地添加了要忽略的文件模式。在这种情况下,您希望忽略所有的 .txt 文件,您可以在 .gitignore 中添加以下内容:

*.txt
  1. 确保 .gitignore 文件已经被提交到仓库中。您可以运行以下命令来检查:
git add .gitignore
git commit -m "Add .gitignore file"
  1. 如果仍然遇到问题,您可以尝试清除 Git 缓存,然后再次运行 git status:
git rm -r --cached .
git add .

这应该解决 .gitignore 文件不起作用的问题。如果问题仍然存在,请确保您的 Git 版本足够新,因为旧版本可能会导致问题。希望这可以帮助您解决问题。

英文:

Hi I'm a beginner to Git and am not sure why .gitignore is not working for me when I create new files. I've seen that Git will continue to track files that had previously been staged/committed in the repo but these are NEW untracked files so shouldn't they be ignored. Please help, thanks!

>>> echo hello > file1.txt
>>> echo file1.txt > .gitignore
>>> git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .gitignore
        file1.txt

Tried to debug by checking git ls-files and double checking the .gitignore contents. However, all seems non-problematic so I'm not sure why the file is not ignored.

I tried adding *.txt in .gitignore too to no avail

I used git check-ignore which returns me nothing, so it suggests that the .gitignore is not implemented properly?

答案1

得分: 1

这是你要求的翻译:

原来你的问题是另一个“gitignore 是 UTF-16 编码”的问题,这在 Windows 上可能会发生。

如何发现它?

一种方法是打开 git-bash 并使用 file 实用程序:

$ file .gitignore
.gitignore: Unicode text, UTF-16, little-endian text

或者在支持编码的编辑器中打开你的 .gitignore 文件(例如 Notepad++ 或 VSCode),查看编辑器检测到的格式。

如何修复它?

从命令行,你可以在 git-bash shell 中使用 iconv

iconv -f UTF-16 -t UTF-8 .gitignore -o .gitignore

或者在支持编码的编辑器中,将文件的编码从 UTF-16 转换为 UTF-8。

英文:

It turns out your issue was another instance of the "gitignore is in UTF-16" issue, which can happen on Windows.

How can I spot it ?

One way is to open git-bash and use the file utility:

$ file .gitignore
.gitignore: Unicode text, UTF-16, little-endian text

or open your .gitignore file in an encoding aware editor (notepad++ or vscode for example) and look at the format detected by the editor.

How can I fix it ?

From the command line, you can use iconv from a git-bash shell:

iconv -f UTF-16 -t UTF-8 .gitignore -o .gitignore

or from an encoding aware editor, convert the encoding of that file from UTF-16 to UTF-8.

huangapple
  • 本文由 发表于 2023年5月15日 13:20:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76251053.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定