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!
Update (or add) the
.gitignorefile 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.
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).
Next, re-add the files with:
git add .
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"