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

22,285

Solution 1

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.

Solution 2

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

Solution 3

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

Share:
22,285
Martin Cooke
Author by

Martin Cooke

Updated on February 02, 2022

Comments

  • Martin Cooke
    Martin Cooke about 2 years

    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?

  • Martin Cooke
    Martin Cooke about 6 years
    Not sure I want to remove any files as such I just don't want to push changes onto my remote repository as I have connection strings inside this file, I presumed what ever was in the .gitignore would be ignored if you made changes to these files.
  • DavidG
    DavidG about 6 years
    You should probablyy split out your connection strings into their own json file then and exclude that one.
  • Martin Cooke
    Martin Cooke about 6 years
    shall give this "git rm --cached <file>" a try then, thanks all :)
  • Alex Sham
    Alex Sham over 3 years
    No one asks to remove file from project folder. OP just wants to remove them from tracking. Incorrect answer.
  • Todd
    Todd over 2 years
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
  • Berkay
    Berkay over 2 years
    sorry @Todd essential parts are in the qoute above... and the link is git-scm.com documentation git ignore notes. it helped me and i wanted to share others because i thought official documentation note points out exact answer. i am a newbei. i was only looking.. this is the first time i answer a question.