简体   繁体   English

默认情况下从 git Commit 中排除 Visual Studio 中的文件

[英]Exclude file in Visual Studio from git Commit by default

Several developers are working on a project and each one has their own connection string in Web.Config.几个开发人员正在处理一个项目,每个开发人员在 Web.Config 中都有自己的连接字符串。 Every once in a while (usually few hours!!) a developer forgets to exclude web.config from their commit and soon everyone has to go though the cycle of getting an error, discovering that its because of wrong connection string, and correct it.每隔一段时间(通常是几个小时!!),开发人员会忘记将 web.config 从他们的提交中排除,很快每个人都不得不经历一个错误的循环,发现是因为错误的连接字符串,并纠正它。 The reason is that when people choose to commit their changes, VS adds all changes to commit.原因是当人们选择提交他们的更改时,VS 会将所有更改添加到提交。 They can exclude it, but they forget.他们可以排除它,但他们忘记了。

Is there a way to exclude a file from commits By Default ?有没有办法在默认情况下从提交中排除文件? Please note that this is different from excluding the file from repository which can be done in .gitignore.请注意,这与从存储库中排除文件不同,后者可以在 .gitignore 中完成。

Ok.好的。 It turns out that a solution to the original problem is possible.事实证明,原始问题的解决方案是可能的。 The complete guide is in this post by Scott Hanselman.完整指南在 Scott Hanselman 的这篇文章中 The basic idea is that you can put part of the configuration data in another file and then refer to that file from web.config.基本思想是您可以将部分配置数据放在另一个文件中,然后从 web.config 中引用该文件。 That file can be excluded from source control in .gitignore as usual.该文件可以像往常一样从 .gitignore 中的源代码管理中排除。

<appSettings file="Web.SECRETS.config">      
    <add key="name" value="someValue" />
</appSettings>

That's not possible to do that with Visual Studio.使用 Visual Studio 无法做到这一点。

You have 2 options... Either, use a better git gui that manage the staging area: Gitextensions or sourcetree.您有 2 个选择……或者,使用更好的 git gui 来管理暂存区:Gitextensions 或 sourcetree。

Or, and that's the solution commonly accepted, use another user config file that is ignored by git (so never accidentally committed ) but where your read the config first if it exists.或者,这是普遍接受的解决方案,使用另一个被 git 忽略的用户配置文件(因此永远不会意外提交),但如果存在,则首先读取配置。 It really worth the little development...真的值得小开发...

There is an even better solution to your problem.有一个更好的解决方案可以解决您的问题。 You can easily create an ASP.NET 5 like configuration yourself, in any .NET application (older versions of ASP.NET, console apps, etc.):您可以在任何 .NET 应用程序(旧版本的 ASP.NET、控制台应用程序等)中轻松地自己创建一个类似于 ASP.NET 5 的配置:

http://dusted.codes/aspnet-5-like-configuration-in-regular-dotnet-applications http://dusted.codes/aspnet-5-like-configuration-in-regular-dotnet-applications

You can save the connection string in Windows Registry.您可以将连接字符串保存在 Windows 注册表中。 This way you can maintain different registry values for different machines (ex: if the different database name is used for test server and production server).通过这种方式,您可以为不同的机器维护不同的注册表值(例如:如果不同的数据库名称用于测试服务器和生产服务器)。

More info: https://my.axerosolutions.com/spaces/5/communifire-documentation/wiki/view/757/sql-connection-string-in-registry更多信息: https : //my.axerosolutions.com/spaces/5/communifire-documentation/wiki/view/757/sql-connection-string-in-registry

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM