My .gitignore doesn’t work

Leon Jalfon
RhinOps by Sela
Published in
1 min readApr 27, 2020

In this post we will see how to resolve this common issue and understand why it happens.

Background

The .gitignore file is used to prevent Git to tracking certain files (namely ensure that certain files not tracked by Git remain untracked), for example test outputs, logs and build files.

The best practice is to create this file when the repository is created, however in most cases, this file is created after detecting that unwanted files are being stored

The Problem

The files/folder that already are in the repository will not just delete themselves just because you added them to the gitignore file. Remember, the .gitignore file ignores only files that are not tracked.

The Solution

To resolve the problem remove from the repository the tracked files contained in the .gitignore file. To achieve this use “git rm” to remove and untrack all the files in the repository, then use “git add” to re-add all the files (the files contained in the .gitignore file will not be added) and finally commit the changes.

git rm -r --cached .
git add .
git commit -m "untrack files contained in the .gitignore file"

--

--