Code & Read

Author: Anjan Bhowmik

Remove a folder from GIT repository by keep locally

Ever had a situation where a folder accidentally ended up in your GIT repository. I am not talking about those folders which are hidden with GIGA bytes sized worth files 😉 These folders should not even be near your repository!

I am talking about innocent but unwanted folders. For example, the .idea folder that is created by lovable PhpStorm IDE that we all love. This folder should definitely not be in GIT! Why you ask? because if it is checked out by another person using PhpStorm as well, it could give them some potential trouble! That’s a big NO NO for you little repo, that you want to share with others. This is my personal experience. Unfortunately from both ends!

So, to remedy this, we have to make sure 2 things. First, I want the folder in question to be gone from GIT repository. Second, most importantly I have to keep this folder on my local, or i have to say bye bye to PhpStorm project config and spend some time to reconfigure it. In worst case, if you are not careful, you may delete an important folder locally, then push it to repo and all hell breaks loose! I have seen quite a few Redditer doing panic posts like “I deleted this folder accidentally, now what do i do?” It may or may not be unrecoverable, but it SURE does give you a solid scare!

Now on to how to purge the folder on repo, but to keep it locally, we just need to do this –

git rm -r --cached .idea

For your case, just replace the .idea with your own folder name. Please DO note that –cached flag! if you skip it, you sure are gonna receive some tough love from your boss or team leader. If none of them are present, you will SURELY want to kick yourself. Why the flare? Its just because, if you missed the –cached flag, and run a command like git rm -f .idea, then you will remove it from repo alright, and as a bonus you will remove from you local filesystem, too!

So, after all that warning out of the way, and git cached version of the folder (The .idea in current cache), how it is time to tell github (or any other git repo) politely that you no longer want that folder there. So, just do a plain old git add and then git commit, this will allow github to update your repo.

That’s it! And, we are done (in a bit!)

P.S. If you want to ask AI about this, be sure to prepare your prompt to something like this –

I have accidentally added .idea folder on my github git repo, now i want to remove it completely from local and remote repo, but want to keep that folder in my local filesystem. how do i do it?

From there you can explore more on the topic. Now, we are truly done!

© 2025 Anjan's Blog

Theme by Anders NorenUp ↑