git 为什么 Visual Studio 在我的 .gitignore 文件中提交文件?

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

Why is visual studio committing files in my .gitignore file?

gitvisual-studio-2013gitignore

提问by seangwright

I'm using Visual Studio 2013 Express using Git on a project.

我在项目上使用 Git 使用 Visual Studio 2013 Express。

I created my repository and copied the project files into it from another directory. I then checked my .gitignoreand .gitattributesfiles. .gitignorewas pre-configured and looked to cover everything I needed to ignore for commits at this point.

我创建了我的存储库并将项目文件从另一个目录复制到其中。然后我检查了我的.gitignore.gitattributes文件。 .gitignore已预先配置,并希望涵盖此时我需要忽略的提交的所有内容。

I selected the Changes tab in Visual Studio under the "Team Explorer" tab and saw all my files were untracked, as expected. I then clicked "Add All". I assumed VS would only add files not matching those in my .gitignoreto the "Included Changes" list. Instead, all the files were added to the "Included Changes" list.

我选择了 Visual Studio 中“团队资源管理器”选项卡下的“更改”选项卡,发现我的所有文件都未按预期进行跟踪。然后我点击了“全部添加”。我认为 VS 只会将与我的文件不匹配的文件添加.gitignore到“包含的更改”列表中。相反,所有文件都被添加到“包含的更改”列表中。

I then thought that maybe the .gitignorefile would be used when committing and that only files not matching those in .gitignorewould be committed. So I added a commit message and clicked "Commit".

然后我想也许在.gitignore提交时会使用该文件,并且只会提交与其中不匹配的文件.gitignore。所以我添加了一条提交消息并单击“提交”。

When I select "View History" from the "Actions" drop down in the "Changes" tab I am presented with a pane to the left of my "Team Explorer" view that shows all the commits I've made. There is only 1 commit. When I double click that commit, the "Team Explorer" tab displays Commit 8db34b1cand all the files in my project are listed there in the tree view with [add] after their name.

当我从“更改”选项卡的“操作”下拉菜单中选择“查看历史记录”时,我会在“团队资源管理器”视图左侧看到一个窗格,其中显示了我所做的所有提交。只有 1 次提交。当我双击该提交时,将显示“团队资源管理器”选项卡,Commit 8db34b1c并且我的项目中的所有文件都列在树视图中,名称后带有 [add]。

For example, App_Data/Movies.mdfand App_Data/Movies.ldfare listed even though my .gitignoreincludes the lines

例如,即使我的包含行App_Data/Movies.mdfApp_Data/Movies.ldf被列出.gitignore

# SQL Server files
App_Data/*.mdf
App_Data/*.ldf

I don't understand how the .gitignore file is being used here or what the "Included Changes", "Excluded Changes" or "Untracked Files" sections mean.

我不明白 .gitignore 文件是如何在这里使用的,也不明白“包含的更改”、“排除的更改”或“未跟踪的文件”部分的含义。

An explanation of the above behavior and sections would be appreciated.

对上述行为和部分的解释将不胜感激。

Edit--

编辑 -

I deleted my .git directory, created a new repo, copied my project files in again and then took screenshots of what I see.

我删除了我的 .git 目录,创建了一个新的 repo,再次复制了我的项目文件,然后截取了我所看到的内容。

Here are images of my .gitignore file and my "Changes" tab.

这是我的 .gitignore 文件和我的“更改”选项卡的图像。

enter image description hereenter image description here

enter image description hereenter image description here

There's this SO questionand this msdn questionwhich shows that other people seem to be having issues as well. I tried deleting the .xml file added by VS in my .git directory (as noted in the msdn question) but that did not solve the issue for me.

this SO questionthis msdn question表明其他人似乎也有问题。我尝试删除 VS 在我的 .git 目录中添加的 .xml 文件(如 msdn 问题中所述),但这并没有解决我的问题。

Edit 2 --

编辑 2——

This is my current directory structure :

这是我当前的目录结构:

/MvcMovie/
    |--.git/
    |
    |---MvcMovie/
    |       |--App_Data/
    |       |     |--Movies.mdf
    |       |     |--Movies.ldf
    |       |     
    |       |--App_Start/
    |       |--etc...
    |
    |--packages/
    |--.gitignore
    |--.gitattributes
    |--MvcMovie.sln
    |MvcMovie.v12.suo

采纳答案by Joel B

@dustinmoris is right in his answer, but the fix is not as immediately obvious as the procedure would lead you to believe. So let me explicitly walk you through the process to make sure the files in your .gitignorefile are obeyed by Visual Studio.

@dustinmoris 在他的回答中是正确的,但修复并不像程序会让你相信的那么明显。因此,让我明确地引导您完成整个过程,以确保.gitignoreVisual Studio 遵守您文件中的文件。

1. Create Your Solution & Add It To Source ControlVisual Studio 2013 Solution Creation

1. 创建您的解决方案并将其添加到源代码管理Visual Studio 2013 Solution Creation

I created a sample MVVM LightXAML Solution and you can see that all the currently untracked files that should ideally be added to the next commit show up with a little green plus next to their icons.

我创建了一个示例MVVM LightXAML 解决方案,您可以看到所有当前未跟踪的文件,理想情况下应该添加到下一次提交中,它们的图标旁边会显示一个绿色的加号。

Solution directory listing

Solution directory listing

The problem is that the MvvmLight.Win81_TemporaryKey.pfxfile is listed to be added even though all files of type .pfxare explicitly called out to be ignored in my .gitignore.

问题是该MvvmLight.Win81_TemporaryKey.pfx文件被列为要添加,即使所有类型的文件.pfx在我的.gitignore.

gitignore

gitignore

This is even more obvious if I select the solution in the Team Explorerpanel and then select the Changesbutton. I see a list of all files pending inclusion on the next commit.

如果我在Team Explorer面板中选择解决方案,然后选择Changes按钮,这将更加明显。我看到了下一次提交中待包含的所有文件的列表。

Team Explorer Changes

Team Explorer Changes

This list contains every file in the Solution, not just the subset specified by the .gitignorefile.

此列表包含解决方案中的每个文件,而不仅仅是.gitignore文件指定的子集。

2. Close Visual Studio & Delete the ms-persist.xmlFile

2. 关闭 Visual Studio 并删除ms-persist.xml文件

It's in a hidden directory so it's kind of hard to find. It should be located at the following path

它在一个隐藏目录中,所以很难找到。它应该位于以下路径

Solution Directory\.git\ms-persist.xml

解决方案目录\.git\ms-persist.xml

ms-persist.xml file

ms-persist.xml file

3. Re-Open The Solution and Commit Files

3.重新打开解决方案并提交文件

You'll notice that it appears as if your solution is no longer under source control because all the icons have lost their green plus signs, but don't worry it's fine.

您会注意到,您的解决方案似乎不再受源代码控制,因为所有图标都丢失了绿色加号,但不要担心,这很好。

Solution directory listing after deleting ms-persist.xml

Solution directory listing after deleting ms-persist.xml

Head on over to the Team Explorerpanel and then select the Changesbutton. Again, you'll see that the project is still being tracked by source control, as well as a list of all files to be added on the next commit. More importantly, you'll notice that your .gitignorelist was properly adhered to this time and all files specified as ignored are no longer listed as pending changes to be added to the next commit. As you can see below, the MvvmLight.Win81_TemporaryKey.pfxfile is no longer scheduled for the next commit!

转到Team Explorer面板,然后选择Changes按钮。同样,您将看到该项目仍在由源代码管理跟踪,以及在下一次提交时要添加的所有文件的列表。更重要的是,您会注意到这次您的.gitignore列表得到了正确的遵守,并且所有指定为忽略的文件不再列为待添加到下一次提交的待处理更改。正如您在下面看到的,该MvvmLight.Win81_TemporaryKey.pfx文件不再安排在下一次提交!

Corrected commit changes

Corrected commit changes

Hope that helps someone. After all that hassle here is my honest advice:

希望能帮助某人。毕竟,这里的麻烦是我诚实的建议:

Learn and use the command line interface since it provides the full range of functionality git offers and has the same interface on all platforms.

学习和使用命令行界面,因为它提供了 git 提供的全部功能,并且在所有平台上都具有相同的界面。

回答by dustinmoris

If anyone ever wonders how to fix it. This is the solution: Go to your .git folder (might be hidden) and open up the ms-persist.xml and you can see the tracked files, excluded items, etc.

如果有人想知道如何修复它。这是解决方案:转到您的 .git 文件夹(可能被隐藏)并打开 ms-persist.xml,您可以看到跟踪的文件、排除的项目等。

Just manually fix what VS has messed up and restart the IDE and you should be good again!

只需手动修复 VS 搞砸的东西并重新启动 IDE,您应该又好了!

Don't delete the whole .git folder like it is often mentioned on other SO threads!

不要像其他 SO 线程中经常提到的那样删除整个 .git 文件夹!

Thanks to Eric Nelson's blog post: https://ericnelson.wordpress.com/2014/06/21/is-visual-studio-2013-ignoring-your-gitignore-file/

感谢 Eric Nelson 的博文:https: //ericnelson.wordpress.com/2014/06/21/is-visual-studio-2013-ignoring-your-gitignore-file/

回答by Edward Thomson

AppData/*.mdfand AppData/*.ldfrefer to .mdfand .ldffiles that exist in the AppDatadirectory that is in the root of your git repository.

AppData/*.mdfAppData/*.ldf引用.mdf.ldf存在于AppDatagit 存储库根目录中的文件。

Your AppDatadirectory actually exists beneath the MvcMoviedirectory. You can add:

您的AppData目录实际上存在于MvcMovie目录之下。你可以加:

MvcMovie/AppData/*.mdf
MvcMovie/AppData/*.ldf`

to your .gitignoreto ignore those specific files, or, if you want to ignore the .mdfand .ldfdirectories in allAppDatafolders throughout your repository:

让您.gitignore忽略这些特定文件,或者,如果您想忽略整个存储库中所有文件夹中的.mdf.ldf目录:AppData

**/AppData/*.mdf
**/Appdata/*.ldf

回答by Cindy K.

If you change the SQL database file specifications in the .gitignore to:

如果将 .gitignore 中的 SQL 数据库文件规范更改为:

# SQL Server files
*.mdf
*.ldf

Wouldn't that ignore all SQL database files throughout the entire solution folder tree, no matter what subfolder it was found in?

这不会忽略整个解决方案文件夹树中的所有 SQL 数据库文件,无论它位于哪个子文件夹中?

回答by Jess

  1. In Team Explorer, go to your solution and then to the changes view.
  2. Select the silly files you want to exclude.
  3. Right click and select Ignore this extension. Team Explorer - Ignore this extension
  4. You will see that there have been changes made to the .gitignore which you can now commit.
  1. Team Explorer 中,转到您的解决方案,然后转到更改视图。
  2. 选择要排除的愚蠢文件。
  3. 右键单击并选择忽略此扩展名Team Explorer - Ignore this extension
  4. 您将看到对 .gitignore 进行了更改,您现在可以提交。

.gitignore

.gitignore

*.ldf
*.mdf

Or you can use the Ignore this local item to only ignore that specific item if you don't want to ignore an extension completely.

或者,如果您不想完全忽略扩展,则可以使用忽略此本地项目仅忽略该特定项目。