git Visual Studio 项目的 .gitignore 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51077976/
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-19 13:09:12  来源:igfitidea点击:

.gitignore for visual studio project is not working

gitvisual-studio

提问by SaintMSent

I am using standard Visual Studio .gitignore file, but Git for Windows still includes build files and other stuff. How to fix this?

我使用的是标准的 Visual Studio .gitignore 文件,但 Windows 版 Git 仍然包含构建文件和其他内容。如何解决这个问题?

Git ignore file I am using: https://github.com/github/gitignore/blob/master/VisualStudio.gitignoreVisual Studio generates the same thing automatically

我正在使用的 Git 忽略文件:https: //github.com/github/gitignore/blob/master/VisualStudio.gitignoreVisual Studio 自动生成相同的东西

It is named correctly, and it is in the right place

它被正确命名,并且在正确的位置

enter image description here

在此处输入图片说明

When I enter git status command it shows build files

当我输入 git status 命令时,它会显示构建文件

enter image description here

在此处输入图片说明

回答by Honza P.

For us worked this approach:

对于我们来说,这种方法是:

  1. Check .gitignore file - seemed ok (bin/Debug folder content ignored)
  2. Check GIT repo for these files (bin/Debug folder content) - they were there => inconsistence with ignore file
  3. Locally delete these files and commit and push (narrow the GIT files with .gitignore definitions)
  4. (Optional) Restart Visual studio and perform Pull
  5. Repository and VS seems now to be in consistent state
  1. 检查 .gitignore 文件 - 似乎没问题(bin/Debug 文件夹内容被忽略)
  2. 检查这些文件的 GIT 存储库(bin/Debug 文件夹内容)-它们在那里 => 与忽略文件不一致
  3. 在本地删除这些文件并提交和推送(使用 .gitignore 定义缩小 GIT 文件)
  4. (可选)重启Visual Studio并执行Pull
  5. 存储库和 VS 现在似乎处于一致状态

回答by stijn

The files are modified, meaning they were already in the git repository beforeyou added the .gitignore file (or you added them explicitly) so you cannot 'ignore' changes to them anymore now.

这些文件已被修改,这意味着它们您添加 .gitignore 文件(或您显式添加它们)之前已经在 git 存储库中因此您现在不能再“忽略”对它们的更改。

If possible, just start over again and create the repository from scratch but now make a first commit which just adds the .gitignore file (which is always a good idea btw), then a second commit adding all source files; the build files will then be ignored.

如果可能,只需重新开始并从头开始创建存储库,但现在进行第一次提交,只添加 .gitignore 文件(顺便说一句,这总是一个好主意),然后第二次提交添加所有源文件;然后将忽略构建文件。

Alternatively you could rewrite the history using interactive rebasing, by modifying the commit in which you add those files, and adding the .gitignore in an earlier commit would also not be bad. Or you could use branch filtering to get rid of all traces of those files. And there's probably other options.

或者,您可以使用交互式变基重写历史记录,通过修改添加这些文件的提交,并在较早的提交中添加 .gitignore 也不错。或者您可以使用分支过滤来消除这些文件的所有痕迹。而且可能还有其他选择。

回答by gpro

It is possible to stop tracking any changes for any checked in file. You need to tell git to remove the file from its index first by calling the following in the given repository > git rm --cached <file>. Then update your .gitignorefile to exclude the file. Finally commit the removal of the file.

可以停止跟踪任何签入文件的任何更改。您需要先通过在给定存储库中调用以下命令来告诉 git 从其索引中删除文件> git rm --cached <file>。然后更新您的.gitignore文件以排除该文件。最后提交删除文件。

Source from Microsoft Git guide.

来自Microsoft Git 指南