简体   繁体   中英

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. 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.

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>

That's not possible to do that with Visual Studio.

You have 2 options... Either, use a better git gui that manage the staging area: Gitextensions or 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. 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.):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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