Untracking ignored files with Git

Learn how to un-track ignored files with Git.

July 7, 2021 - 2 minute read -

The Problem

While working on a project, you may forget to add or update the .gitignore file from time to time. It’s probably not at the top of your agenda when you’re diving into a new project. This can lead to various files being accidentally committed from time to time (your build directories, caches, etc).

This is not good, especially if you’ve committed some high-churn files!

The Solution

  1. Update (or add) the .gitignore file so that the correct files are excluded from being tracked. This by itself isn’t enough as it only provides Git with rules when tracking new files. For this step, I usually generate the file using gitignore.io.

  2. In your terminal, run:

    git rm -r --cached .

    This will remove all files from the Git index (don’t worry, they haven’t gone anywhere).

  3. Next, re-add the files with:

    git add .
  4. Finally, commit them with an appropriate message:

    git commit -am "Untracked ignored files"

While re-committing those new files, you should see that any that were identified in your .gitignore file were not included.

You may find yourself going through this routine fairly often if you’re working on a lot of projects, so it may be worth remembering this one-liner:

git rm -r --cached . && git add . && git commit -am "Remove ignored files"