How To Remove a File From Git Without Deleting It

Submitted by Daniel Henry on 06/04/2013 - 12:01:pm

A handy little snippet I was in need of the other day. I forgot to put a couple of cache aggregate file directories into my .gitignore file and realized I needed to get that out so the files that are generated every 24 hours are not all filling up my repo for no reason. I found the answer somewhere in stack overflow and have kept the snippet around in case I needed it again.

First, add the directory to your .gitignore file. Then, call this command against the files (not the directory).

  1. git rm --cached <file>

Keep in mind that when you check in the change even though it will not remove the file from your current host, if you pull from another repo that references the same parent repo it will delete that file as part of the merge. So you can only keep the file on one server.

Blog photo credit to   Hyde Bons, Zack Rusin, and Sebastien Pierre.