CrazyLegs
CrazyLegs

Reputation: 608

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

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?

Upvotes: 42

Views: 53344

Answers (4)

adamency
adamency

Reputation: 1500

Every answer in this thread misses the point: Being able to ignore changes on a tracked file.

You do not want to completely untrack this file as this would make you send the deletion of the item on the remote next time you push and thus delete the file for every of your collaborators, which you obviously do not want.

What you're looking for is actually perfectly possible in git, while a bit hidden:

git update-index --assume-unchanged  <file>

which will precisely ignore the changes on a tracked file.

Now you can modify your appsettings.json file all you want and git won't bother you with it, and won't upload the changes when you push to the remote.

Upvotes: 14

Berkay
Berkay

Reputation: 341

This is the official reference of git look at here it says:

The purpose of gitignore files is to ensure that certain files not tracked by Git remain untracked.

To stop tracking a file that is currently tracked, use git rm --cached

Upvotes: 4

DavidG
DavidG

Reputation: 118937

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

Upvotes: 13

simone.tino
simone.tino

Reputation: 2464

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.

Upvotes: 45

Related Questions