Remove tracked ignorned files from repo but not locally

I keep running into a scenario where someone on our team pushes an initial commit without first adding a .gitignore to their project. This results in a bunch of files ending up in the repo that we don’t want tracked.

git ls-files -i --exclude-from=.gitignore
gives me a list of files that are ignored by .gitignore

  • Ignore everything except one directory in .gitignore
  • Git - in config file, what changes if I init or clone?
  • What is the best way to make the same change to multiple branches?
  • How to revert to changes I committed?
  • Undoing checkout in GIT
  • Git bare - what to backup?
  • and

    git rm --cached <file>
    lets me remove files one at a time from the repo, but keeps them in my working directory (which I want)

    Is there a way I can pipe the file list from ls-files to rm --cached (or some other method altogether that will allow me to remove the tracked, ignored files from my repo)?

    One of our team members wrote a shell script that uses regex to do it, but I’m looking for a command-line only solution (if one exists).

  • unable to connect to cache daemon?
  • How to get Visual Studio to Fetch incoming commits automatically
  • capistrano problem
  • Git submodules with separate work_tree
  • Is there any difference between `git gc` and `git repack -ad; git prune`?
  • Git submodule push
  • One Solution collect form web for “Remove tracked ignorned files from repo but not locally”

    You can try:

    git rm --cached $(git ls-files -i --exclude-from=.gitignore)
    

    (Following the same idea than what works for deleted files in
    “git remove files which have been deleted”, or in “git: how to add/commit removals made via vanilla rm?”, or “Removing multiple files from a Git repo that have already been deleted from disk”).

    Or: a simple pipe could work too:

    git ls-files -i --exclude-from=.gitignore | xargs -0 git rm --cached
    
    Git Baby is a git and github fan, let's start git clone.