Sometimes you have a project with some files that don’t need to be tracked in version control. To exclude a file from being tracked in Git, you add it to the .gitignore file. But what if the file you want Git to ignore is already being tracked? Well, a file that’s already being tracked will not be ignored, even if you add it to .gitignore. So, how do we tell Git to ignore it? A few easy steps will do the trick.
Commit any pending changes you may have:
git commit -m "Some comment"
Clear the file from cache and commit again:
git rm --cached [filename]
git commit -m "Cleared [filename] from cache."
Now open .gitignore
, add [filename]
to it and save.
Stage your changes and commit again:
git add .
git commit -m "[filename] should be ignored now."
Test it by modifying the ignored file, then git status
. You should see that the file is not being tracked anymore and you’re good to go.