How to remove folder from Git after .gitignore

Andrew Kirchofer Feb 27, 2023

As a developer, you may have encountered situations where you accidentally added a folder or file to your Git repository that you did not intend to track. This can cause issues when trying to push or pull changes and can also bloat your repository with unnecessary files. Luckily, you can easily remove a folder from Git after adding it, even if you have already added it to your .gitignore file. In this blog post, we will go through the steps required to accomplish this.

Step 1:
Remove the folder from Git The first step is to remove the folder from Git using the following command:

git rm -r --cached folder-name

This command removes the folder named "folder-name" from Git, but leaves it in your local filesystem. The "--cached" flag ensures that the files are removed from Git, but not from your local filesystem.

Step 2:
Commit the changes Once you have removed the folder from Git, you need to commit the changes using the following command:

git commit -m "Removed folder-name from Git"

This command creates a new commit with a message indicating that the folder has been removed from Git.

Step 3:
Push the changes Finally, you need to push the changes to the remote repository using the following command:

git push origin branch-name

This command pushes the changes to the remote repository on the branch named "branch-name".

At this point, the folder has been removed from Git and will no longer be tracked by Git. However, the folder may still exist in your local filesystem. If you want to remove the folder from your local filesystem as well, you can simply delete it using your operating system's file explorer or the command line.

Tired of managing GitHub issues manually? Genius is the ultimate AI-powered GitHub project management tool.