Git: remove files from repository only

by Viktor Garske on Nov. 19, 2017, midnight


This is a translated and adapted version of the original article (German).


There are situations when you want to delete files or directories in the repository only, e.g. when they are added to .gitignore.

The classic git rm would delete files from the working tree or local file system, too. The solution is the option --cached.

When you want to delete a file (e.g. file_to_delete) from the repository only you can use the command

git rm --cached file_to_delete

The command for directories looks similar (except you need the -r option):

git rm --cached -r dir_to_delete/

Don't forget to commit and push.

git commit -m "Deleted file"
git push


Pro-Tip: if you want to delete all files matching the .gitignore fromt the repository you can use

git rm --cached `git ls-files -i -X .gitignore`


Source: 1, 2, Git logo: Jason Long (CC-BY)

Author image
Viktor Garske

Viktor Garske ist der Hauptautor des Blogs und schreibt gerne über Technologie, Panorama sowie Tipps & Tricks.

Comments (0)

Comments are not enabled for this entry.