If your GIT repository is tracking files that you want to ignore (i.e. you init your repo without .gitignore file) then these files still be present in you repository index also after adding them to the just created .gitignore file.
Step 1 – Create (or modify) .gitignore file
Step 2 – Commit all your changes
After .gitignore file has been saved, commit all you files, including your new .gitignore file.
git commit -m “Fixed .gitignore issue.”
Step 3 – Remove everything from the repository
Clear your repo by using:
git rm -r –cached .
rm is the remove command
-r will allow recursive removal
–cached will only remove files from the index. Your files will still be there.
The . indicates that all files will be untracked. You can untrack a specific file with git rm –cached foo.txt (thanks @amadeann).
The rm command can be unforgiving. If you wish to try what it does beforehand, add the -n or –dry-run flag to test things out.
Step 4 – Re-Add everything
git add .
Step 5 – Commit
git commit -m “.gitignore fix”
Step 6 – Push
Push the changes to your remote to see the changes effective there as well.
Useful .gitignore template: