How to Remove Cached Files from Git

Screenshot of default .gitingore file for Next.js

I suddenly realized that I committed a file or directory to git which I did not want to be committed. Now what do I do? Git provides a nice way to remove files from the local git cache without removing them from my local repository. Here is the structure of the git command to do this:

git rm [-r] --cached <path-to-directory-or-file>

For example, if I use any JetBrains IDE there will be a .idea directory placed at the local repository root. If I have committed this directory to git by mistake, I can remove it by first adding ".idea" to .gitignore and then running the following command:

git rm -r --cached .idea