How to Remove a File from a Git Repository (with git rm)
Short answer
The git rm command removes a file from the Git repository and, by default, also deletes it from the filesystem. To remove a file from both, run git rm <file>. To stop tracking a file without deleting it from disk — for example, to add it to .gitignore after accidentally committing it — use git rm --cached <file>. To remove an entire directory recursively, add the -r flag: git rm -r <directory>. Before removing many files at once using a glob pattern, use git rm --dry-run <pattern> to preview which files would be affected without actually deleting anything. After running git rm, stage and commit the removal as you would any other change.
The git rm command removes a file from the Git repository. Unlike simply deleting a file in your editor or file browser — which only removes it from the filesystem — git rm also stages the removal so it's included in your next commit.
The Git Cheat Sheet
No need to remember all those commands and parameters: get our popular "Git Cheat Sheet" - for free!
Free. We email you a ZIP with our cheat sheets.
Remove a File from the Repository and Filesystem
To delete a file from both the Git repository and the disk:
git rm file1.txt
Stop Tracking a File Without Deleting It
Use --cached to remove a file from Git's tracking while keeping it on your filesystem. This is useful when you accidentally committed a file you now want to add to .gitignore:
git rm --cached file2.txt
Remove a Directory Recursively
Add -r to remove a folder and all its contents:
git rm -r old-directory/
Preview Before Removing
When removing multiple files with a glob pattern, use --dry-run (or -n) to see what would be deleted without actually doing it:
$ git rm css/* --dry-run
rm 'css/about.css'
rm 'css/general.css'
No Command Line Needed
Removing Files in Tower
In case you are using the Tower Git client, removing files is very easy: select the "Move to Trash" option and Tower will automatically mark the file as "deleted" in Git. And if you change your mind, hit CMD+Z (or CTRL+Z on Windows) to restore the file!
In Tower, you can use this keyboard shortcut to effortlessly undo almost any action in Git, making it easy to revert changes.