35

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?

Stevie Howard
  • 904
  • 9
  • 12
Martin Cooke
  • 526
  • 1
  • 4
  • 18
  • 2
    Possible duplicate of [How to make Git "forget" about a file that was tracked but is now in .gitignore?](https://stackoverflow.com/questions/1274057/how-to-make-git-forget-about-a-file-that-was-tracked-but-is-now-in-gitignore) – phd Apr 19 '18 at 22:01

4 Answers4

40

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.

smn.tino
  • 2,272
  • 4
  • 32
  • 41
12

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
DavidG
  • 113,891
  • 12
  • 217
  • 223
  • 1
    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. – Martin Cooke Apr 19 '18 at 22:10
  • You should probablyy split out your connection strings into their own json file then and exclude that one. – DavidG Apr 19 '18 at 22:11
  • 2
    No one asks to remove file from project folder. OP just wants to remove them from tracking. Incorrect answer. – Alex Sham Dec 17 '20 at 08:19
9

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.

adamency
  • 682
  • 6
  • 13
4

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

Berkay
  • 301
  • 2
  • 7
  • 1
    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. – Todd Sep 03 '21 at 18:55
  • 1
    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. – Berkay Sep 03 '21 at 23:20