Clear already committed files but included in .gitignore
suggest changeA file can stay tracked after you add it to .gitignore. Ignore rules do not untrack files that are already in the index, so the old file remains in the repository until you remove it from the index.
One fix is to remove everything from the index, then add the files back. This is not a dry run: it changes the index. It is safe only when you have no pending changes you need to keep separate, because git add . stages the working tree, and only if you pass --cached so the files stay on disk. The final git push assumes the branch is master and that you want to publish the removal.
# Remove everything from the index (the files will stay in the file system)
$ git rm -r --cached .
# Re-add everything (they'll be added in the current state, changes included)
$ git add .
# Commit, if anything changed. You should see only deletions
$ git commit -m 'Remove all files that are in the .gitignore'
# Update the remote
$ git push origin master
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents