Removing Ignored Files Previously Committed in Git Project

Sometimes when working on a project we decide to update the .gitignore file with files that have already been committed and pushed into the repository. THis is a guide on how to get those files out of the repository.

This guide assumes you have no important changes uncommitted.

Run this gets up to date with master

git reset --hard origin/master

Make any desired updates to the .gitignore file at this point and then run

git clean -df
git checkout -- .

git clean removes all the untracked files in your working directory, -d removes any files and also any subdirectories that become empty as a result, and -f means force. git checkout – . clears out the working directory.

Run

git rm -r --cached .
git add -A
git commit -am 'Removing ignored files'

This rips out all the associated files mentioned in the .gitignore, and commits the changes locally.

You can now run

git add . 
git push

The files added to the ignore should now be gone resulting in a cleaner and more lightweight git repository.

Share