Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using .gitignore file to hide appsettings.json does not actually hide it

Tags:

I have a C# MVC .Net Core application I'm building, the connection string is in a file called appsettings.json so what I want to do is simply exclude this from my git repository. I have added the following line to the git ignore file:

appsettings.json

I have also tried:

**/appsettings.json

But neither seem to work, the change I've made to the appsettings.json file still appears, am I missing something fundamental here?

like image 899
Martin Cooke Avatar asked Apr 19 '18 21:04

Martin Cooke


People also ask

Can you encrypt Appsettings JSON?

It is totally possible and quite easy to encrypt it. We use environment variables to override the values in appsettings. json in production. The environment variables are loaded from Kubernetes constructs called ConfigMaps (for non-sensitive data) and Secrets (for sensitive data).

Which files should I ignore in git?

gitignore should list the names or name-patterns of files that will be found in work-trees when working with your project, but that should not be committed to the project. In other words, it's not OS-specific, it's project-specific.

What is the purpose of the Appsettings JSON file?

The appsettings. json file is generally used to store the application configuration settings such as database connection strings, any application scope global variables, and much other information.


2 Answers

This is a common misunderstanding about the way .gitignore works we all met at some point when working with Git: .gitignore will ignore all files that are not being tracked yet; indeed, files that are already being tracked in your Git repository are not ignored by your .gitignore setup.

To fulfil your need, it would be sufficient to untrack the files that you desire to ignore, i.e. in your case the appsettings.json file. As reported in your question's comments, this has been answered already here. Then, your .gitignore setup will work as you would expect.

like image 161
smn.tino Avatar answered Sep 23 '22 06:09

smn.tino


Adding an entry to your .gitignore file won't remove any files that have already been added to your repository. You need to remove them manually. For this you can use the rm command:

git rm --cached project/appsettings.json 
like image 43
DavidG Avatar answered Sep 25 '22 06:09

DavidG