How to Remove a File from a Git Repository (with git rm)
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!
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'
Tip
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.
Get our popular Git Cheat Sheet for free!
You'll find the most important commands on the front and helpful best practice tips on the back. Over 100,000 developers have downloaded it to make Git a little bit easier.
About Us
As the makers of Tower, the best Git client for Mac and Windows, we help over 100,000 users in companies like Apple, Google, Amazon, Twitter, and Ebay get the most out of Git.
Just like with Tower, our mission with this platform is to help people become better professionals.
That's why we provide our guides, videos, and cheat sheets (about version control with Git and lots of other topics) for free.