Applying .gitignore Change to a Repository

A great feature of git is the ability to mark certain file and folders as being excluded from the repository which is done via entries in the .gitignore file.

One issue I regularly have is when things change and I need to update the .gitignore file and I always forget how to apply that change. That’s because it is something that isn’t obvious or something that I do regularly. Fortunately it is incredibly easy to achieve and here’s how…

How to apply .gitignore changes

Assuming that you have already edited and made your changes to .gitignore the next this to do is to commit any changes that you have waiting.

git rm -r --cached .

This will remove everything from git giving you a message to the console for every file. Do not be alarmed as we are about to re-add everything.

The next stage is the same as when you are initially setting up a repository from an existing folder. The steps are as follows:

  • add everything back to git
  • do a commit
  • push the changes

I have shown this as all three commands issues in one line but you can break these down and run separately if needed.

git add . && git commit -m 'Update of .gitignore' && git push

And that’s it. The repository will now be up-to-date with the changes to .gitignore applied.

Leave a Reply

Your email address will not be published. Required fields are marked *