Git - Remove Previously Committed Files From Being Tracked

When working with VSCode and using the debugger VSCode automatically adds a .vscode folder to your repository.

MyAwesomeModule
    │   MyAwesomeModule.psm1
    │
    └───.vscode
            extensions.json
            launch.json
            settings.json
            tasks.json

This however isn’t always required to be committed to source control but you may not have added this to your .gitignore at the start of your project and now everyone that clones the repo will have your VSCode settings, not ideal if that’s not what you intended.

Solution

Create a .gitignore file in the root of your directory

MyAwesomeModule
    │   .gitignore
    │   MyAwesomeModule.psm1
    │
    └───.vscode
            extensions.json
            launch.json
            settings.json
            tasks.json

Add an entry .vscode to the gitignore file. You will see the .vscode folder go a darker colour in your explorer pane which means the change has been recognised and git will not track the folder.

Run the following to remove the .vscode folder from tracking.

git rm -r --cached .\MyAwesomeModule\.vscode

This will remove the folder from this point forward in the repository but it will still be visible in previous commits.

A good StackOverflow post on the variations of tracking configuration files local & remote.

Comments