Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 246
Alireza Avatar asked Oct 12 '15 11:10

Alireza


People also ask

How do I enable Git ignore in Visual Studio 2017?

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.

How do I exclude a file from a Git project?

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

How do I ignore a specific file in Git?

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.

How to exclude TFS changes in Git from Visual Studio?

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.


Video Answer


3 Answers

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>
like image 175
Alireza Avatar answered Oct 13 '22 06:10

Alireza


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

like image 28
dustinmoris Avatar answered Oct 13 '22 08:10

dustinmoris


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

like image 1
Harsh Avatar answered Oct 13 '22 07:10

Harsh