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.

30 responses to “Remove Files from Git After Adding/Updating .Gitignore”

  1. Very nice and clear, thanks.

    1. Hey there Uncert! Thanks for leaving a comment to let me know that you found this helpful. Also, I apologize for my slow reply.

  2. Thank you! Very useful for me, a Git beginner 🙂

  3. Thank you. Worked perfectly.

  4. Super fast and helpful, thanks for posting!

    1. Thanks for letting me know it was helpful!

  5. Looks pretty useful! If I use these commands, would the repository lose its history?

    1. The repository wouldn’t lose any history that had been committed and pushed to a remote repo. But, I believe it would lose any local history that was not pushed.

  6. This was exactly what I needed, thanks for writing this (and showing up in Google)!

  7. Well explained and it works fine. Thanks 🙂

  8. Thanks for the info! I understand that with these commands, files in the newly updated gitignore are no longer tracked. However, what happens to the files (that are now ignored) in the previous commits? Are the files removed from the previous commits, or do they remain?


    1. Those files will still be in your commit history. So, if it’s something sensitive, like keys, you’ll want to do some cleanup with others methods.

      This article has proved helpful to me in the past:

  9. Perfect, thanks!

  10. Short and crisp. Thanks for the help.

  11. Worked perfectly! Thanks

  12. Thanks a lot, It worked flawlessly.

    1. You’re welcome! I’m glad it worked out for you.

  13. Finally working solution after I stuck with the gitignore 🙂

  14. Thanks a lot for this short, clear and very helpful post.

  15. Thanks!!!

  16. This is great, thank you!

  17. Success!!!! I thought I was going to be at this for another few hours trying to remove the files that shouldn’t have been tracked. This took all of 5 minutes. I will be bookmarking this for future under my WordPress peeps folder. Thanks Eric!

  18. Very concise for helping to remove pre-pushed node_modules folderthank you very much!

  19. Awesome!
    Thanks 🙂

  20. Great one thank you

  21. worked just fine – thanks

  22. Thanks you very much, this worked like a charm.

  23. Still great after all these years, I call this kind of info “seldom used, often missed”.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.