Git – Remove a file from a Git repository without deleting it from the local filesystem

delete-filegitgit-rmremote-serverrepository

My initial commit contained some log files. I've added *log to my .gitignore, and now I want to remove the log files from my repository.

git rm mylogfile.log

will remove a file from the repository, but will also remove it from the local file system.

How can I remove this file from the repo without deleting my local copy of the file?

Best Answer

From the man file:

When --cached is given, the staged content has to match either the tip of the branch or the file on disk, allowing the file to be removed from just the index.

So, for a single file:

git rm --cached mylogfile.log

and for a single directory:

git rm --cached -r mydirectory