Remove directory from remote repository after adding them to .gitignore
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
When you realize that a directory you’ve committed and pushed to your remote Git repository contains files that should not be tracked (like build outputs, logs, or dependencies), you first step is often adding that directory to your .gitignore file. However, this action only prevents the files from being tracked in future commits; it doesn’t remove them from the repository’s history. This can be problematic for several reasons such as bloating the repository size, exposing sensitive information, or simply cluttering your repository with unnecessary data.
Understanding .gitignore
The .gitignore file in a Git repository is a means to specify intentionally untracked files to ignore. When files specified in .gitignore are involved in the repository, git is configured to ignore them in future commits. However, if these files were already tracked by Git before they were added to .gitignore, they will continue to be tracked.
Steps to Remove Directories from Remote Repository
To remove directories from the history of your git repository you need to:
- Modify the
.gitignorefile - Remove the files from the repository
- Commit and push the changes to the remote repository
Step 1: Update the .gitignore
If not already done, add the directory you want to exclude to the .gitignore file:
Step 2: Remove the files from the repository
To remove the files from your repository while retaining your local copies, use the following command:
For example:
The --cached option tells Git to remove the paths from the staging area and the index without affecting the working directory.
Step 3: Commit and push the changes
Commit the changes with a message explaining what you’ve done:
Finally, push the changes to your remote repository:
Why Should You Remove Directories from a Git Repository?
Keeping unwanted directories can lead to several issues:
- Repository size: Large directories, especially those that contain external libraries or dependencies like
node_modules, can significantly bloat the size of your repository, making cloning and fetching slower. - Security: Sensitive configurations or data might be exposed through such directories.
- Maintenance: Cleaner repositories are easier to manage.
Additional Considerations
- Impact on Collaborators: Ensure that collaborators pull the changes immediately after you remove directories from the repository. This helps avoid merge conflicts.
- Backup: Always ensure to have a proper backup of your data before performing operations that affect git history.
Summary Table
| Task | Commands | Description |
Update .gitignore | echo 'node_modules/' >> .gitignore | Adds node_modules directory to .gitignore |
| Remove directory | git rm -r --cached node_modules/ | Removes node_modules directory from git tracking, but not from local file system |
| Commit changes | git commit -m "Remove node_modules" | Commits the changes to the repository |
| Push changes | git push origin main | Updates the remote repository with the changes |
Conclusion
Proper management of your .gitignore and understanding how to retroactively clean your repository can lead to more maintainable, secure, and efficient Git practices. Always ensure to keep bleeding information or unnecessary bloat out of remote repositories by timely cleaning up and applying .gitignore appropriately.

