What is git rm? (Quick Introduction)
The git rm command is used to remove files or directories from both the working directory and the Git repository. When you delete a file using git rm, Git tracks the removal and stages it for the next commit.
In simple terms, git rm helps you safely delete tracked files while keeping the change in version history. If you only want to stop tracking a file but keep it locally, you can use the --cached option.
git rm Cheat Sheet (Commands & Use Cases)
| Description | Command |
|---|---|
| Remove a tracked file | git rm file.txt |
| Remove multiple files | git rm file1.txt file2.txt |
| Remove a directory recursively | git rm -r folder/ |
| Force remove (ignore changes) | git rm -f file.txt |
| Force remove directory | git rm -rf folder/ |
| Remove file from Git but keep locally | git rm --cached file.txt |
| Remove directory from Git but keep locally | git rm -r --cached folder/ |
| Untrack all files in a folder | git rm -r --cached folder/ |
| Remove files matching pattern | git rm *.log |
| Remove ignored files (force required) | git rm -f ignored.log |
| Remove file already staged | git rm file.txt |
| Undo staged deletion before commit | git restore file.txt |
| Restore deleted file after commit | git checkout HEAD~1 file.txt |
| Remove file only from index (like git reset) | git rm --cached file.txt |
| Remove files listed in .gitignore (cleanup) | git rm -r --cached . |
| Remove file from previous commit (via revert) | git revert <commit> |
| Remove untracked files (alternative) | git clean -fd |
| Check what will be removed before execution | git rm -n file.txt |
Common git rm Commands
Remove file using git rm
Use the git rm command to delete a tracked file from both the working directory and the Git repository. This is the safest way to remove files because Git records the deletion in version history.
git rm file.txtRemove directory using git rm -r
To delete a folder and all its contents, use the recursive option -r. This ensures all tracked files inside the directory are removed properly.
git rm -r folder/You can also explore more file operations in Git delete file or directory explained.
Remove forcefully using git rm -rf
If the file has local changes or is staged, Git may prevent deletion. In such cases, use the force option -f along with recursive deletion.
git rm -rf folder/Use this carefully, especially when working with important branches. Learn safer workflows in Git workflow explained.
Untrack file using git rm --cached
Use --cached when you want to remove a file from Git tracking but keep it in your local system. This is commonly used for files added by mistake or sensitive files.
git rm --cached file.txtFor a deeper understanding, check Git remove file from tracking.
Real-World Scenarios
Delete a tracked file from Git
When a file is no longer needed in your project, use git rm to remove it cleanly. This ensures the deletion is tracked and shared with your team after commit.
git rm config.old
git commit -m "Remove unused config file"Remove file but keep it locally (git rm --cached)
If you accidentally committed a file (like .env or logs), you can untrack it without deleting it locally.
git rm --cached .env
git commit -m "Stop tracking .env file"Pair this with .gitignore for better control. See
Gitignore examples.
Remove folder recursively (git rm -r)
When cleaning up project structure, you may need to delete entire directories.
git rm -r logs/
git commit -m "Remove logs directory"Force delete ignored or modified files
Sometimes Git blocks deletion due to changes. Use -f to override safely when you are sure.
git rm -f file.txtIf you want to discard changes instead, refer to Git discard changes.
Remove files already pushed to remote
If a file is already pushed to a remote repository, removing it requires committing the deletion and pushing again.
git rm secret.txt
git commit -m "Remove sensitive file"
git pushIf sensitive data was exposed, consider rewriting history using advanced methods in Git remove commit guide.
Undo git rm
Restore deleted file before commit
If you have used git rm but have not committed the changes yet, you can easily restore the file using the git restore command. This brings the file back from the staging area to your working directory.
git restore file.txtThis is the safest and quickest way to undo accidental deletions before committing changes.
Restore file after commit using git checkout
If the deletion is already committed, you can recover the file from a previous commit using git checkout.
git checkout HEAD~1 -- file.txtThis restores the file from the previous commit. You can also specify any commit hash if needed.
For more details on navigating commits, refer to Git HEAD, caret (^) and tilde (~) explained.
Undo git rm using git restore
In modern Git versions, git restore is the recommended way to undo file deletions or changes.
git restore --source=HEAD file.txtThis restores the file from the latest commit without affecting other changes.
You can explore more in Git restore command explained.
Recover deleted files using git reflog
If you are unsure where the file existed, use git reflog to view all recent changes and recover lost commits.
git reflog
git checkout <commit-id> -- file.txtThis is useful when files are deleted after multiple operations or resets.
Learn more in Git reflog tutorial.
git rm --cached Explained
What does git rm --cached do?
The git rm --cached command removes a file from Git tracking but keeps it in your local filesystem. It is commonly used when a file was mistakenly added to the repository.
git rm --cached file.txtAfter running this command, the file becomes untracked but still exists locally.
When to use git rm --cached
Use git rm --cached in scenarios like:
- Removing sensitive files (e.g.,
.env, secrets) - Stopping tracking of log or build files
- Cleaning up files that should be in
.gitignore
git rm --cached .env
git commit -m "Remove .env from tracking"You can combine this with .gitignore. See
Gitignore examples.
git rm vs git rm --cached
The key difference between git rm and git rm --cached is how they affect your local files.
| Command | Effect |
|---|---|
git rm file.txt | Removes file from Git and deletes it locally |
git rm --cached file.txt | Removes file from Git but keeps it locally |
For related cleanup commands, check Git clean command explained.
rm -rf Command vs git rm -rf
What is rm -rf command?
The rm -rf command is a Linux/Unix command used to forcefully delete files and directories recursively from the filesystem.
rm -rf folder/This command does not involve Git and permanently deletes files without tracking.
Why rm -rf is dangerous
The rm -rf command is powerful but risky because:
- It deletes files permanently without confirmation
- It does not maintain any history
- Mistakes cannot be easily undone
For example:
rm -rf *This can wipe entire directories instantly, so always double-check before executing.
git rm -rf vs rm -rf comparison
Although both commands remove files recursively, they behave very differently.
| Command | Behavior |
|---|---|
git rm -rf folder/ | Removes files from Git and filesystem, tracks deletion |
rm -rf folder/ | Permanently deletes files from filesystem only |
If you accidentally use rm, Git will detect missing files but cannot restore them automatically unless they exist in history.
To safely manage deletions and history, also review Git reset examples and Git revert command explained.
Frequently Asked Questions
1. What does git rm do?
The git rm command removes files from both the working directory and the Git repository. It deletes tracked files and stages the deletion for the next commit.2. What is the difference between git rm and rm?
The rm command deletes files from the filesystem only, while git rm removes files from both the filesystem and the Git repository, allowing the change to be tracked in version history.3. What does git rm --cached do?
The git rm --cached command removes a file from Git tracking without deleting it from the local filesystem. It is useful when you want to keep the file locally but stop tracking it in Git.4. How do I undo git rm?
You can undo git rm before committing usinggit restore <file> or after committing by checking out the file from a previous commit using git checkout <commit> -- <file>.5. What is rm -rf command?
The rm -rf command forcefully deletes files and directories recursively from the filesystem without confirmation. Unlike git rm, it does not track changes in Git.Common Errors and Fixes
error: the following file has changes staged in the index
This error occurs when you try to remove a file that has staged or modified changes. Git prevents deletion to avoid accidental data loss.
To fix this, you can either commit the changes first or force the removal using the -f option.
git rm -f file.txtAlternatively, if you want to discard changes safely, use:
git restore file.txtFor more ways to handle staged changes, refer to Git unstage files explained.
fatal: pathspec did not match any files
This error usually means the file you are trying to remove does not exist in the current directory or is not tracked by Git.
Common reasons include:
- Typo in file name
- File already deleted
- File is untracked
To verify, check the current status:
git statusIf the file is untracked, you can remove it using:
rm file.txtOr clean all untracked files using:
git clean -fdLearn more in Git remove untracked files.
git rm not working
If git rm is not working as expected, check the following:
- Ensure the file is tracked (
git status) - Verify correct file path
- Check for local modifications (use
-fif needed)
Example:
git rm -f file.txtIf your goal is only to untrack the file (not delete it locally), use:
git rm --cached file.txtFor alternative approaches, see Git reset examples and Git restore command explained.
Summary
The git rm command is used to remove files from both the working directory and the Git repository while tracking the deletion in version history.
- Use
git rmto delete tracked files safely - Use
git rm -rto remove directories - Use
git rm --cachedto untrack files without deleting them locally - Use
git rm -fwhen files have staged or modified changes - Avoid using
rm -rffor tracked files, as it bypasses Git tracking
Understanding when and how to use git rm helps you manage repositories more effectively and avoid accidental data loss.
Official Documentation
For complete and authoritative details on the git rm command and related topics, refer to the official Git documentation:
- git rm Documentation
- git restore Documentation
- git reset Documentation
- git clean Documentation
- git reflog Documentation
These resources provide detailed explanations, advanced options, and edge cases directly from the Git maintainers.



![Git Error: Cannot Delete Branch Checked Out or Used by Worktree [SOLVED]](/cannot-delete-branch-checked-out-at/git-cannot-delete-branch_hu_3f86bd25a59d627d.webp)






