Posted on August 19, 2015
Hey Git, why don’t you ignore my recently added .gitignore paths?
In projects with a Git source control you often need to tell your repository to ignore changes to specific files that you don’t want to upload to the server and spread them over to other team members. Typical examples are usually IDE related settings or publish information. For this approach, Git provides the
.gitignore file where users can enter paths to those files they want to have ignored by the repository. But sometimes this does not seem to work.
In most cases the
.gitignore does not work for the same reason: The files you want to ignore have already been added to the repository once. Since then, Git watches about their changes, no matter if they have been added to the
.gitignore afterwards or not. To ignore already added files afterwards we need to help ourselves by using a trick.
The shortest way to archive this is removing all files from the git repository and adding them immediately afterwards. This sounds like a huge intervention but is is not that heavy as it seems.
Important: Please commit all outstanding changes to the repository before doing this. Otherwise they will get lost.
git rm -r --cached .
git add .
git commit -m ".gitignore is now working"
.gitignore is working as it should be and you get rid of unwanted files and folders in your repository.