Remove a directory from Git or stop tracking it

Delete a tracked directory, retain local files with git rm --cached, restore staged deletions, or plan a separate history cleanup.

Best practices
Illustration of Git branches and version history

Use git rm -r -- directory/ to remove a tracked directory's files from both the working tree and the index, then commit the deletion. Use --cached if you want to keep the local files while removing them from future snapshots.

Neither operation erases files from earlier commits. Removing historical content is a separate repository rewrite.

Delete a tracked directory

git rm -r -- old-module/
git commit -m "Remove obsolete module"

Git checks for changes that would be lost and can refuse the removal. Inspect and save those changes rather than immediately adding --force.

Keep local files but stop tracking them

git rm -r --cached -- logs/
printf '\n/logs/\n' >> .gitignore
git add .gitignore
git commit -m "Stop tracking runtime logs"

The root-anchored ignore pattern prevents new files under the root logs/ directory from being added normally. The cached removal leaves files in your current checkout, but collaborators who receive the deletion can have their previously tracked copies removed. Tell them to preserve local data first.

Remove a directory from history

For repository-size cleanup or historical sensitive content, work in a fresh clone with an untouched backup and a coordinated rewrite plan:

git filter-repo --path old-module/ --invert-paths

git-filter-repo is installed separately. Path filtering does not automatically follow historical renames: include every old path that must be removed. A full rewrite normally removes origin; inspect the result before restoring a destination and updating affected branches and tags. Rewriting only the current branch may leave the content reachable elsewhere.

Collaborators must migrate away from the old history without merging it back. For exposed credentials, revoke or rotate them immediately; removing Git history cannot revoke copies in forks, caches, or existing clones.

FAQ

Does git rm remove empty directories?

Git does not record empty directories as standalone entries. Once all tracked files are removed, the directory disappears from the committed tree. Untracked files may keep it present on disk. Inspect git clean -nd -- directory/ before considering removal of those remaining files.

Why did the repository not get smaller?

Older commits still reference the deleted files. History cleanup, removal of other retaining refs, and eventual object cleanup are needed to reclaim storage. Hosting services may retain additional objects or cached views; use their documented cleanup process rather than expecting an ordinary deletion commit to shrink the repository.

How do I undo the deletion?

For an uncommitted deletion staged by git rm, restore both the index and working tree from HEAD:

git restore --source=HEAD --staged --worktree -- old-module/

Plain git restore . reads the index, where git rm has already removed the files, so it is not the correct undo command. After the deletion was committed, restore from a known earlier commit and create a new commit:

git restore --source=HEAD~1 --staged --worktree -- old-module/
git commit -m "Restore module"

Use HEAD~1 only when that commit contains the version you need. Restoration overwrites changes at the selected paths, so inspect them first.

See git-rm, git-restore, and git-filter-repo.

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting