How to Remove Git Cached Files After Adding to .gitignore

git

The following bash script can help you removing your cached files on git if you add your file into .gitignore and GIT is already tracking them.

        
        
          
            
          
          
            
          
        

        #!/bin/bash
 
git ls-files -i --exclude-standard | while IFS= read -r file; do
    echo "Removing cached file: $file"
    git rm --cached "$file"
done
    IFS=$OLD_IFS
  fi
done

      

Let's breakdown this bash commands:

Step 1: git ls-files

  • This command lists files in the git repository
  • By default, it shows all files that are currently tracked (in the git index)

Step 2: The -i flag

  • -i stands for "ignored"
  • This modifies the behavior to show files that would be ignored by git's ignore rules
  • However, it only shows ignored files that are currently tracked

Step 3: The --exclude-standard flag

  • This tells git to use the standard ignore sources:
    • .gitignore files (in current and parent directories)
    • .git/info/exclude file (repository-specific ignores)
    • Global gitignore file (configured in core.excludesFile)

Step 4: What the combination does

git ls-files -i --exclude-standard finds files that are:

  1. Currently tracked by git (in the index/cache)
  2. Would be ignored if they weren't already tracked
  3. Match patterns in any of the standard ignore sources

Step 5: The pipeline

        
        
          
            
          
          
            
          
        

        | while IFS= read -r file; do
      
  • Takes each filename from the previous command
  • IFS= prevents word splitting on spaces in filenames
  • read -r prevents backslash interpretation

Step 6: The removal

        
        
          
            
          
          
            
          
        

        echo "Removing cached file: $file"
git rm --cached "$file"
      
  • Shows which file is being removed
  • git rm --cached removes the file from git's index but keeps it in the working directory

When to use this?

Let's say you have:

  • A tracked file: logs/debug.log
  • You later add to .gitignore: logs/*.log

What happens:

  1. git ls-files -i --exclude-standard finds logs/debug.log because:
    • It's currently tracked
    • It matches the new ignore pattern logs/*.log
  2. The script removes logs/debug.log from git's cache
  3. The file stays in your working directory but is now ignored by git

This is the typical "oops, I forgot to ignore this before committing it" cleanup scenario.

Creating a bash script

So, create a .sh file on your folder and copy the content above.

        
        
          
            
          
          
            
          
        

        vi remove_cached.sh

      

Save your file and add x permission:

        
        
          
            
          
          
            
          
        

        chmod +x remove_cached.sh

      

Then run your script:

        
        
          
            
          
          
            
          
        

        ./remove_cached.sh

      

After run this script you shouldn’t see files you want to ignore under your .gitignore. You can delete the .sh file afterwards.

Source: shell script, How to run a script on mac