How Do I Remove a Directory from a Git Repository?

Better Stack Team
Updated on July 25, 2024

To remove a directory (including all files and subdirectories within it) from a Git repository while preserving it locally on your filesystem, you'll need to follow these steps:

Step-by-Step Guide

1. Remove Directory from Git Repository

To remove a directory from Git while keeping it locally, you should use git rm with the -r (recursive) and --cached options:

 
git rm -r --cached path/to/directory
  • Explanation:
    • git rm: This command removes files from the working tree and the index (staging area).
    • r: Recursively removes all files and subdirectories.
    • -cached: This option ensures that the files are only removed from the index (staging area) and not from the working directory.
  • Example:

     
    git rm -r --cached assets/images
    

2. Commit the Removal

After removing the directory from the Git repository's index (staging area), commit the changes to make them permanent in your local repository:

 
git commit -m "Remove directory assets/images from repository"

3. Verify Changes

Verify that the directory has been removed from the Git repository. Running git status can help confirm that the removal has been staged and committed correctly:

 
git status

Notes

  • Preserving Locally: Using git rm --cached ensures that the directory and its contents are kept locally on your filesystem but are no longer tracked by Git.
  • Careful: Be cautious when using git rm as it permanently removes files and directories from Git's history. Make sure you are removing the correct directory.
  • Pushing Changes: After committing the removal, if you want to push these changes to a remote repository, use git push.

Example Scenario

Let's say you want to remove a directory named images located at assets/images from your Git repository:

 
git rm -r --cached assets/images
git commit -m "Remove directory assets/images from repository"
git push origin <branch-name>

Replace <branch-name> with your branch name and make sure to push the changes if needed.

Additional Considerations

If you want to remove a directory completely from both your local filesystem and Git repository (including its history), you can use git rm -r without --cached. However, be very careful with this command as it will delete the directory and its contents permanently from both Git and your local filesystem.

 
git rm -r path/to/directory

Always ensure you have backups or copies of important files before executing commands that delete or modify data irreversibly.

Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github