Several developers are working on a project and each one has their own connection string in 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. The reason is that when people choose to commit their changes, VS adds all changes to commit. 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.
In the Visual Studio, click Git > Settings. This opens the Options window. Navigate to Source Control > Git Repository Settings. In the Git files section, click Add (next to Ignore file ). It’ll add the .gitignore file to the repository root directory. The UI will change to indicate that the .gitignore now exists.
Excluding a file is simple. In the root folder of your source controlled project is a folder named .git. (Note the period on the front, also note that on some operating systems it may be hidden by default.) Under it is another folder called info. In .git/info is a file called exclude (with no extension).
From the top menu select Git > Settings. The above will open Visual Studio’s Options with Source Control > Git Global Settings selected. From the list on the left select Git Repository Settings and then click the Add button for Ignore file. The above will add a.gitignore file with all the proper files ignored for a typical Visual Studio setup.
When we used TFS control, we were able from Visual Studio to just right-click the file and choose "Exclude" and it would appear in the "Excluded changes" list and not being checked in with the changeset. But that's not possible in Git. At least not from Visual Studio.
Ok. It turns out that a solution to the original problem is possible. The complete guide is in this post by 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. That file can be excluded from source control in .gitignore as usual.
<appSettings file="Web.SECRETS.config">
<add key="name" value="someValue" />
</appSettings>
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.):
http://dusted.codes/aspnet-5-like-configuration-in-regular-dotnet-applications
You can save the connection string in Windows Registry. 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With