git 为什么 .gitignore 不忽略我的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45400361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:05:11  来源:igfitidea点击:

Why is .gitignore not ignoring my files?

gitvisual-studio-codegitignore

提问by Richard

See the image below. My .gitignore file should be ignoring all files in src/dist, but isn't.

见下图。我的 .gitignore 文件应该忽略 src/dist 中的所有文件,但不是。

enter image description here

在此处输入图片说明

回答by Elmar Peise

.gitignoreonly ignores files that are not part of the repository yet. If you already git added some files, their changes will still be tracked. To remove those files from your repository (but not from your file system) use git rm --cachedon them.

.gitignore只忽略不属于存储库的文件。如果您已经git add编辑了一些文件,它们的更改仍将被跟踪。要从您的存储库(但不是从您的文件系统)中删除这些文件,请使用git rm --cached它们。

回答by MarckK

The .gitignore file ensures that files not tracked by Git remain untracked.

.gitignore 文件可确保 Git 未跟踪的文件保持未跟踪状态。

Just adding folders/files to a .gitignore file will not untrack them -- they will remain tracked by Git.

只是将文件夹/文件添加到 .gitignore 文件不会取消跟踪它们——它们将被 Git 跟踪。

To untrack files, it is necessary to remove from the repository the tracked files listed in .gitignore file. Then re-add them and commit your changes.

要取消跟踪文件,必须从存储库中删除 .gitignore 文件中列出的跟踪文件。然后重新添加它们并提交您的更改。

The easiest, most thorough way to do this is to remove and cache all files in the repository, then add them all back. All folders/files listed in .gitignore file will not be tracked. From the top folder in the repository run the following commands:

最简单、最彻底的方法是删除并缓存存储库中的所有文件,然后将它们全部添加回来。.gitignore 文件中列出的所有文件夹/文件都不会被跟踪。从存储库的顶部文件夹运行以下命令:

git rm -r --cached . git add .

git rm -r --cached . git add .

Then commit your changes:

然后提交您的更改:

git commit -m "Untrack files in .gitignore"

git commit -m "Untrack files in .gitignore"

Please note that any previous commits with the unwanted files will remain in the commit history. When pushing to GitHub be aware of a commit history that might contain .envor client_secret.jsonfiles.

请注意,任何先前提交的不需要的文件都将保留在提交历史记录中。推送到 GitHub 时,请注意可能包含.envclient_secret.json文件的提交历史记录。

Best practiceis to create a .gitignore file and populate it with the folders/files you do not want tracked when starting a project. However, often it is necessary to add to the .gitignore file after realising that unwanted files are being tracked and stored.

最佳做法是创建一个 .gitignore 文件,并在启动项目时使用您不想跟踪的文件夹/文件填充它。但是,在意识到正在跟踪和存储不需要的文件后,通常需要将其添加到 .gitignore 文件中。

回答by Igal S.

gitignore ignores only untracked files. Your files are marked as modified - meaning they were committed and the past and their are now tracked by git.

gitignore 仅忽略未跟踪的文件。您的文件被标记为已修改 - 意味着它们已提交,过去和现在都由 git 跟踪。

To ignore them, you first need to delete them, git rmthem, commit and then ignore them.

要忽略它们,您首先需要删除它们,git rm它们,提交然后忽略它们。

回答by Philippe Raemy

Look at this : .gitignore is not workingAnd particularly the remark from ADTC:

看看这个:.gitignore 不起作用特别是 ADTC 的评论:

Make sure your .gitignore file uses ANSI or UTF-8 encoding. If it uses something else like Unicode BOM, it's possible that Git can't read the file. – ADTC Dec 14 '17 at 12:39

确保您的 .gitignore 文件使用 ANSI 或 UTF-8 编码。如果它使用 Unicode BOM 之类的其他东西,则 Git 可能无法读取该文件。– ADTC 2017 年 12 月 14 日 12:39

回答by sam r

You can use git rm -r --cached ./node_modules . Suppose if you want to ignore node

您可以使用 git rm -r --cached ./node_modules 。假设你想忽略节点