Remove Files from Git After Adding/Updating .Gitignore

I recently inherited a project from a beginning developer. After inheriting the project I realized that, while the developer was using Git to source control the project, the developer had completely forgot to add a .gitignore.

This meant that I now needed to add a .gitignore file as well as remove files from git that were tracked that shouldn’t have been tracked.

This isn’t usually a big deal when ignoring a single file or two — The command to remove a single file is:

git rm --cached <file>

But, since we use Grunt and Sass for our web development projects, there were a ton of files within node_modules  and .sass_cache  as well as .DS_Store files throughout.

To get around writing multiple commands to ignore all of these files and un-track them, I did this:

git rm -r --cached .
git add -A
git commit -am 'Removing ignored files'

The first command will un-track all files in your git repository.

The second command will then add all of the files in your git repository, except those that match rules in your .gitignore. Thus, we have un-tracked several files with just two commands.

Then the last command is to commit the changes, which will just be removed files.

9 thoughts on “Remove Files from Git After Adding/Updating .Gitignore

Leave a Reply