'Gitignore: Ignore all .vscode directories, but include the top-level launch.json

I want to ignore all .vscode directories that may show up in my repository, except for the top-level one. In that top-level one I want to ignore all files except for the launch.json.

I tried to no extent:

**/.vscode/
!/.vscode/
/.vscode/*
!/.vscode/launch.json
**/.vscode/
!/.vscode/
!/.vscode/launch.json
**/.vscode/
!/.vscode/launch.json
**/.vscode/*
!/.vscode/launch.json
**/.vscode/
/.vscode/!launch.json


Solution 1:[1]

I stumbled on this answer and as some sources today claim that its a good idea to commit the launch.json I wanted to provide the correct answer:

.vscode
!.vscode/launch.json

(You added a slash after !, so the path was considered absolute).

Solution 2:[2]

I'd recommend simply ignoring all of these directories:

.vscode/

and then manually tracking the file you want:

git add -f .vscode/launch.json

The -f adds files even if they're ignored, and once the file is tracked the ignore has no effect on it. Git will see changes to .vscode/launch.json and you'll be prompted to commit them just like any other file.

Solution 3:[3]

Simply put these 2 lines in your .gitignore

.vscode/*
!.vscode/launch.json

First line means, ignore .vscode folder with all (using * symbol after /) of its files. Second line means, exclude (using !) launch.json file whereas inside the .vscode folder

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Thorben
Solution 2 Chris
Solution 3