简体   繁体   中英

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.

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.

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

**/.vscode/

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM