How do I remove a directory from a Git repository?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Removing a directory from a Git repository has two distinct meanings: removing it from tracking (so Git stops versioning it) while keeping it on your local filesystem, or removing it from both Git and your disk. The git rm -r --cached command handles the first case, and git rm -r handles the second. Understanding the difference prevents accidental data loss.
Remove from Git Only (Keep Local Files)
After this, my-directory/ still exists on your local machine but Git no longer tracks it. Other collaborators who pull this change will have the directory deleted from their working copy.
The --cached flag tells Git to remove the files from the staging area (index) only, not from the working tree.
Remove from Git AND Local Disk
Without --cached, git rm -r deletes the files from your disk AND removes them from Git tracking.
Adding to .gitignore After Removal
If you removed a directory from tracking but want to prevent it from being re-added:
The order matters. .gitignore only affects untracked files. If the directory is still tracked, .gitignore has no effect. Remove it from tracking first, then add the ignore rule.
Common Use Cases
Remove build artifacts
Remove IDE configuration
Remove accidentally committed secrets
Note: This removes the directory from the current commit, but it still exists in Git history. To purge it from all history, you need git filter-branch or BFG Repo-Cleaner.
Removing from All Git History
If the directory contains sensitive data (API keys, passwords), removing it from the latest commit is not enough because it still exists in previous commits:
Preview Before Removing
The --dry-run flag shows exactly which files would be affected.
Removing a Single File
Same flags as directory removal, just without -r.
Checking What Git Tracks
Common Pitfalls
- Forgetting
--cached: Runninggit rm -r my-directory/without--cacheddeletes files from your disk. Use--cachedwhen you want to keep local files. - Not adding to
.gitignore: Aftergit rm --cached, the untracked directory will show ingit status. If someone runsgit add ., it gets re-added. Always update.gitignore. - Thinking
.gitignoreremoves tracked files:.gitignoreonly prevents adding new untracked files. It does not remove already-tracked files. You mustgit rm --cachedfirst. - Force pushing after history rewrite:
git filter-branchand BFG rewrite commit hashes. All collaborators must re-clone or rebase. Coordinate with your team before force pushing. - Large directories in history: Removing
node_modules/from the current commit reduces the repo's working tree but not its.gitdirectory size. The old data persists in history until you rewrite history and rungit gc.
Summary
git rm -r --cached dir/removes from Git tracking but keeps local filesgit rm -r dir/removes from both Git and your local filesystem- Always add the directory to
.gitignoreafter removing from tracking to prevent re-addition - Use
--dry-runto preview what would be removed - For sensitive data in history, use BFG Repo-Cleaner or
git filter-branchto purge from all commits .gitignoreonly affects untracked files. It cannot remove already-tracked directories
Related reading
- How do I remove a directory from a Git repository?
- How do I remove a single file from the staging area (undo git add)?
- How do I remove a single file from the staging area undo git add?
- How do I remove a submodule?
- How do I remove files saying old mode 100755 new mode 100644 from unstaged changes in Git?
- How do I remove files saying old mode 100755 new mode 100644 from unstaged changes in Git?
- How do I remove local untracked files from the current Git working tree?
- How do I remove msysgit's right click menu options?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.