How to Use “git Remove” Without Deleting the Files

Nicholas Cerminara
👁️ 37,729 views
💬 comments

If you use Git, you're probably adding files all the time as you develop your project out further and further. Sometimes though, you might need to actually remove a file from the Git repository but not not from your local files. For example, say you forgot to add a file to .gitignore but you don't want to remove it from your local development environment. This could be from a mistake or you didn't realize that a new package or something created a bunch of nasty log files you don't want in the repo. Either way, this is actually very easy to do.

Removing a single file with Git without deleting it

This method will remove a single file from your Git repository without deleting the file from your local environment. Then when you run git push, the files will be removed in the remote repo.


git rm --cached filexample.txt

Removing multiple files with Git without deleting them

In a similar fashion, you can do this for multiple files at one time.


git rm --cached file1.txt file2.txt file3.txt

Removing an entire directory without deleting the files

This also works recursively for folders so long that you add the -r flag to the command. Here's an example:


git rm -r --cached folder