Git: remove files from repository only



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)

Keine Kommentare

Kommentar verfassen