Remove files from GIT repository that was tracked but are now in .gitignore

During project development period, I was frequently facing problems with the files that needs to be ignored but already mistakenly tracked. That files keeps growing by time and occupies big amounts of space in storage even when you delete them on next commits.

.gitignoreprevents untracked files from being added (without an add -f) to the set of files tracked by git, however, git will continue to track any files that are already being tracked.

To stop tracking a file you need to remove it from the index. This can be achieved with this command.

git rm --cached <file>

The removal of the file from the head revision will happen on the next commit.


To remove all cached files, instead of a specific file, use the recursive -r flag along with .. Be sure to re-add all of the files that you do want tracked using git add .

Example:

# Remove all cached files to ensure there are no .gitignore files being tracked
git rm --cached -r .

# Track the files that should be tracked
git add .

Tural Aliyev

I'm traveloholic, musicphile, technoholic, cinephile, workaholic software developer

Istanbul http://tural.us

Subscribe to Tural's Notebook

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!