Remove Files From Git Repo as in Gitignore

After you committed/pushed files to GIT repository, if you decide to ignore some files/directories using .gitignore then they won’t be automatically removed from git repository. Following command can be used for removing those ignored files from repo.

TL;DR

git rm -r --cached .
git add .
git commit -m ".gitignore fix"
git push

Detailed Explanation

To remove the the ignored files/dirs from repo, use GIT remove command as follows.

git rm -r --cached .
Command Arg Detail
git rm Remove command
-r Remove recursive
-cached Your file system is uneffected but it removes the files only in you .git cache
. Include all files. if you want to remove a specific file then use git rm --cached xyz.txt

Now add the all files except those mentioned in .gitignore by using this command

git add .

Commit your changes

git commit -m ".gitignore fix"

and push

git push

Now check your server repository, it shouldn’t have the files mentioned in .gitignore

Note:

  • You can execute the above mentioned remove command with --dry-run option to see what will happen. i.e. Adding this flag will not make any change but it will tell you what it is going to do.