gpt4 book ai didi

git - 使用 git 和 visual studio 忽略对 App.config 的更改

转载 作者:太空狗 更新时间:2023-10-29 14:44:21 29 4
gpt4 key购买 nike

我有一个项目,我使用 git 进行源代码控制,最终使用 github。在项目中,我有一个包含连接字符串名称和连接字符串的 app.config 文件。

在我的 .gitignore 内我有app.config这似乎阻止了 app.config从 checkin 。但是,我注意到如果我从 github 中 pull 出并尝试运行,这将导致新机器上的运行时错误。这是由于应用程序依赖 app.config 中的连接字符串.为了缓解这种情况,我将我的连接字符串更改为“假”只是为了拥有 key ,需要提供值。

我 checkin 了这个“ stub ”app.config,但现在我的真实 app.config 值正在被跟踪,并且希望在每次提交时都被 checkin 。

我有办法阻止这种情况发生吗?我基本上想保留我在源代码管理中的 stub app.config,同时将我的“真实”app.config 保留在本地,而不跟踪对其进行的其他更改。现在的问题是,在每次提交时我都必须记住“排除”app.config,以免我的连接字符串(我不想公开)被检查到源中。

注意,据我所知,我目前没有在机器上安装 git shell(我很惊讶它没有安装 VS)并且没有访问安装软件。除了安装 git shell 和运行 git rm --cached <file> 之外,我还有其他选择吗? ?

最佳答案

试一试。我有一个类似的问题,并且正在使用我们团队内部的工具来修改配置设置并使它们不受源代码控制,但是现在我正在处理完全公开的 repo 协议(protocol),我将不得不进行此更改,并且这是我将要使用的方法(从明天开始,届时我打算对此进行测试:)

http://johnatten.com/2014/04/06/asp-net-mvc-keep-private-settings-out-of-source-control/

基本上,总结一下(应 Op 的要求,但我仍然认为这篇文章最好详细地讲述了所有内容):

Use the File Attribute to Move Select Application Settings to an External File

You may have a case, [...] in which most of the values in the [appSettings] Configuration Section are global to the project, but also include a handful of settings which should remain private, and kept out of source control.

In these cases, there is a special file attribute available specifically to the [appSettings] section which essentially allows us to extend [appSettings] to an external file. In other words, ConfigurationManager will recognize the contents in both locations when referring to [appSettings] and make all transparently available within the application.

In our example case, we have an email password we would like to keep private. We might add another Web Configuration file named PrivateSettings.config. Once again, there should be no XML header. The only thing this file should contain will be a set of elements, and within those, the special settings we wish to define privately.

Special PrivateSettings.config File Extends AppSettings Section:

<appSettings>
<add key="MAIL_PASSWORD" value="xspbqmurkjadteck"/>
</appSettings>

Now, we remove the email password element from Web.config, and add the file attribute to the section element, pointing to the new PrivateSettings.config file:

Add File Attribute to Web.config AppSettings:

<appSettings file="PrivateSettings.config">
<add key="owin:AppStartup" value="AspNetIdentity2ExtendingApplicationUser.Startup,AspNetIdentity2ExtendingApplicationUser" />
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

Again, as before we can access any of our settings in the standard manner – externalizing the email password setting to a separate file is transparent to client code:

Accessing Settings:

var pwd = ConfigurationManager.AppSettings["MAIL_PASSWORD"];

Then, final step: Add Special Files [ie. the private settings] to .gitignore

关于git - 使用 git 和 visual studio 忽略对 App.config 的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36913362/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com