Skye Brown
Skye Brown

Reputation: 497

How to fix .vscode tracking in gitignore

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.

Upvotes: 42

Views: 58593

Answers (10)

Ruveros
Ruveros

Reputation: 1

If none of the answers here worked, try changing .gitignore encoding to UTF-8.

Upvotes: 0

SurveyLoophole
SurveyLoophole

Reputation: 71

I used git-filter-repo to remove the offending file.

First, confirm that the file has been (likely accidentally) commited at some point:

git log --all -- **/example.txt

If entries appear, install git-filter-repo, make a backup of your repo, and (as per the cheat sheet) run

git filter-repo --invert-paths --path-glob 'myDirectory/example.txt' --path 'example.txt'

The path should be path relative to the repo root directory.

I think you can do this also, to remove all files named example.txt

git filter-repo --invert-paths --path-glob '**/example.txt' --path 'example.txt'

Upvotes: 0

comodoro
comodoro

Reputation: 1566

This has happened to me just a while ago in a completely fresh project (folders in gitignore were not ignored by git even before initial commit).

It turned out that the .gitignore file was in the UTF16-LE encoding. I had generated it in the terminal that happened to be Poweershell; vscode read it as a normal text file, so I had no suspicion. After converting to ASCII the problem went away.

Upvotes: 2

Dave Everitt
Dave Everitt

Reputation: 17866

None of the above worked for me as the offending folder (a Nanoc site with an "output" folder) was outside the git repository (work that one out!), so I removed the folder from the directory with the git-tracked files, created an empty new one with the same name in the same location, checked that it was no longer tracked by VSCode. then dragged all the files back into the newly-ignored folder from the original "output" folder.

Upvotes: 0

Michael_313
Michael_313

Reputation: 131

If you committed a file inside the .vscode directory before, and now you are adding .vscode into your .gitignore file, it is not going to ignore the whole directory, because there is a commit associated with one of the files in this directory.

However, any new files inside that directory will be ignored.

Upvotes: 2

Black Beard
Black Beard

Reputation: 1608

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

Upvotes: 3

Parvez Ahammed
Parvez Ahammed

Reputation: 93

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.

Upvotes: 6

SushiGuy
SushiGuy

Reputation: 1627

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)
...

Upvotes: 2

Raul
Raul

Reputation: 961

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.

Upvotes: 59

m0j0
m0j0

Reputation: 3864

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

**/.vscode/

Upvotes: 55

Related Questions