28

I'm having trouble using .gitignore with my .vscode folder in my repository.

I added a rule in my .gitignore to ignore my entire .vscode/ folder:

# Visual Studio Code # 
.vscode/* 
.vscode/settings.json
!.vscode/settings.json 
!.vscode/tasks.json 
!.vscode/launch.json 
!.vscode/extensions.json 
.history.vscode/settings.json

Despite this, .vscode is still being tracked by my local git repository. I've tried a variety of different solutions including using git filter-branch to remove this folder from my git history and git rm --cached .vscode to clear my cache and re-commit my changes, but none of these solutions prevents the .vscode folder from being tracked by git.

When I run git status I keep getting this result:

On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .vscode/

nothing added to commit but untracked files present (use "git add" to track)

So far, I've worked around this by manually excluding this folder when I git add my changes.

Why is .vscode/ still being shown as untracked? If I were to git add . and git commit this, it would add this folder to my git history and eventually to my remote repository when I git push. I don't want this behavior to occur.

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Skye Brown
  • 357
  • 1
  • 5
  • 13
  • 3
    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) – EncryptedWatermelon Jul 24 '19 at 16:59
  • 2
    Your .gitignore file seems contradictory. You have ".vscode/settings.json", which means ignore that file, and then the next line you have the same thing preceded with an exclamation mark, which includes it again. – m0j0 Jul 24 '19 at 17:24
  • @m0j0 yes, I did this to explicitly try to ignore my settings.json file within .vscode when I was looking for different solutions – Skye Brown Jul 24 '19 at 17:56

5 Answers5

45

Had similar problem, turned out the files were added to my cache. Clearing it with below command worked.

git rm --cached .vscode/settings.json

Reference to the issue on github where I found the solution.

Raul
  • 800
  • 1
  • 7
  • 22
38

Maybe try this in your .gitignore. This should ignore the entire .vscode directory, no matter where it is located.

**/.vscode/
m0j0
  • 3,484
  • 5
  • 28
  • 33
2

Happened for an Untracked git file .vscode/settings.json. Turned out to be a second line in our huge .gitignore file that was overriding the effort. In example below simply remove the bottom line.

Remember to re-stage and commit .gitgnore after the change.

# IDEs and editors
.settings/
.vscode/settings.json   -- Line I added that "was not working"
...

# IDE - VSCode
.vscode/*
!.vscode/settings.json  -- Line discovered that was overriding above (REMOVE)
...
SushiGuy
  • 1,573
  • 17
  • 20
0

As there can be three types of files(tracked, untracked, and ignored). The files which are already tracked or indexed can't be ignored just by adding them to the .gitignore. Rather their index should be deleted. Then if you add them in the .gitignore it will be ignored.

Suppose you have a tracked file in the root named test.txt now you are adding this file to the .gitignore. It will not work unless you remove it from the git index like below

 git rm --cached test.txt

Also, you can use the relative path of a file to remove it if they are not in the root.

Then the index will be deleted and git will stage them as files deleted but they won't be deleted from your local.

0

I had the same problem and nothing from the mentioned here helped me. Finally I found the reason:

Essentially, a .gitignore list does not work to ignore files that are already committed into the Git repository. This means that if you:

  1. Make a commit to your Git repository, and then;
  2. Set up your .gitignore list.

Then the .gitignore list will NOT ignore files that are already tracked by your repository. Hence, to avoid this, you’ll want to ensure .gitignore is set up before you make any commits.

The source is here

Black Beard
  • 1,130
  • 11
  • 18