How to Delete a Folder in Git (Locally and Remotely)

When working with Git, managing files and folders efficiently is essential. Sometimes you may need to delete an entire folder—whether it’s obsolete code, build artifacts, or temporary files. Git tracks changes to your project structure, so folder deletion needs to be done properly to reflect in version control.

In this guide, we’ll walk you through how to delete a folder in a Git repository, both locally and in the remote repository (e.g., GitHub or GitLab).


📁 Deleting a Folder in Git

In Git, folders are not tracked directly—only the files inside them are. This means that to remove a folder, you need to delete all files inside it and then commit that change.


✅ Step-by-Step: Delete a Folder in Git

🔹 1. Delete the Folder Locally

Using the terminal:

rm -r folder-name/

On Windows (Git Bash):

rm -rf folder-name/

Or manually delete the folder from your file explorer or code editor.


🔹 2. Stage the Deletion

Tell Git that you’ve removed files:

git add -u

Or more specifically:

git add folder-name/

This stages the deletion for commit.


🔹 3. Commit the Change

git commit -m "Delete folder-name directory"

This records the removal in your Git history.


🔹 4. Push to Remote Repository

git push origin branch-name

Replace branch-name with the branch you’re working on (e.g., main, develop).


🚫 Deleting a Folder Without Deleting It from the Filesystem

If you want Git to stop tracking a folder but not delete it locally, use:

  1. Add the folder to .gitignore
  2. Run:
git rm -r --cached folder-name/
git commit -m "Stop tracking folder-name"
git push origin branch-name

This removes the folder from version control but keeps it on your local system.


🧠 Best Practices

  • Always review what you’re deleting with: git status
  • Avoid deleting folders like .git, node_modules, or venv unless you know what you’re doing.
  • Use .gitignore to prevent unnecessary folders from being tracked in the first place.

✅ Summary

TaskCommand
Delete folder locallyrm -r folder-name/
Stage the deletiongit add -u or git add folder-name/
Commit the changegit commit -m "Delete folder"
Push to remotegit push origin branch-name
Stop tracking but keep localgit rm -r --cached folder-name/

🚀 Final Thoughts

Deleting folders in Git isn’t just about removing files—it’s about maintaining a clean, accurate project history. By properly staging and committing folder deletions, you keep your repository organized and collaborative-ready.

Sharing Is Caring:

Leave a Comment