Skip to content

How to remove a file from git repo but keep it in the local filesystem?

Published:

Let’s assume you accidentally committed and pushed a file into a git repository that doesn’t belong there. Of course, this file should have been included in the .gitignore. But sometimes people make mistakes. Now there is a repository on the server in which this file is included. How do I get it remove it from the repository?

First of all you should add this file to the .gitignore now.

Then use the following commands:

To remove a single file use:

git rm --cached <path/to/file>

To remove a folder use:

git rm --cached -r <path/to/folder>

If you run “git status” now, you should see that the file is marked with “deleted”, but in you filesystem it’s still there.

Write a new commit in which you correct your error and push it to the remote repository. The file should now have been removed from the repository but still exist locally on your system.