gitvisual-studiogithubvisual-studio-2015

Ignoring changes to App.config working with git and visual studio


I have a project that I'm source controlling with git, and eventually github. Within the project I have an app.config file that contains a connection string name and connection string.

Within my .gitignore i have app.config which seems to have prevented the app.config from being checked in. However, I noticed that this would then cause run time errors on a new machine, should I pull from github and attempt to run. This was due to the application relying on the connection string within the app.config. To alleviate this, I changed my connection string to a "fake" just to have the key, the value needs to be provided.

I checked in this "stub" app.config, but now my real app.config values are being tracked, and wants to be checked in with each commit I do.

I there a way to stop this from happening? I basically want to keep the stub app.config I have in source control, while keeping my "real" app.config local, without tracking additional changes to it. The problem right now is, at each commit i have to remember to "exclude" the app.config, lest my connection string (which I don't want to be public) gets checked into source.

Note, to my knowledge, I do not currently have git shell installed on the machine (I'm surprised it was not installed with VS) and do not have access to install software. Do I have any options, aside from installing git shell and running git rm --cached <file>?


Solution

  • Give this a spin. I have a similar problem, and was using a tool internal to our team to modify config settings and keep them out of source control, but now that i am working on what will be entirely public repos, i will have to make this change, and its the approach that I will be using (starting tomorrow, when I intend to test this out :)

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

    Basically, to summarise (as requested by the Op, but I still think the article tells it all nicely in detail the best):

    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