How to Delete Folders from Git repos |Video upload date:  · Duration: PT2M42S  · Language: EN

Quick guide to remove folders from a Git repo safely including staging removal committing pushing and history rewrite options

What this does and why

So you added a folder full of secrets or junk and now you want it gone like it never existed. This guide shows the practical steps to remove a tracked folder from a Git or GitHub repo and when you need to go nuclear and scrub that folder from git history. No miracles here just correct commands and a small amount of regret management.

Remove a tracked folder and push the change

If the folder is only in the latest commits you do not need to rewrite history. Remove it from the working tree and stage the removal with git rm. Commit with a clear message so future archaeologists do not misinterpret your motives.

git rm -r path/to/folder
git commit -m 'Remove unwanted folder'
git push origin main

If the branch is protected follow your team rules first. A forced change to a protected branch is a good way to get paged at 2am.

When you need to wipe the folder from history

When the folder appears in older commits you must rewrite git history. That changes commit ids and will force collaborators to rebase or reclone. Use modern tools for speed and safety and always work on a mirror copy first.

Using git filter repo for a surgical scrub

git filter repo is fast and reliable for removing paths from an entire repository timeline. Work against a mirror clone so you do not break your main copy while testing.

git clone --mirror git@example.com repo.git
git filter-repo --path path/to/folder --invert-paths
git push --force --all

Using the BFG for large files and folders

BFG can be simpler when you have many large files to excise. It wraps a lot of the git plumbing and often finishes faster.

bfg --delete-folders foldername repo.git
git push --force --all

Notes on older tools and why they exist

git filter-branch still works but it is slow and fiddly. Use it only if you have a reason to stick to the old ways. For most jobs use git filter repo or BFG instead.

Warnings and best practices

  • Create a mirror clone before any history rewrite. Test your commands on the mirror so you do not spend the rest of the week apologizing.
  • Rewrite history will change commit ids. Expect collaborators to rebase or reclone their forks and branches.
  • Protected branches may block pushes. Follow your team policy and coordinate changes to avoid surprise outages.
  • Notify your team and schedule a window for the work if the repo is actively used. This is not the time to be a lone cowboy.

Final tip

Removing a folder locally is easy. Removing it from history is a serious change to your version control timeline. Back up, test on a mirror, and tell your coworkers before you force push. If anything goes wrong you will have the backup and a perfectly reasonable excuse to blame a mysterious script.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.