The short answer: use git rm --cached, then add the directory to .gitignore

When Git is tracking a directory you want to stop tracking, you have two separate problems to solve. First, you need to tell Git to stop watching it — that's git rm --cached. Second, you need to prevent Git from re-adding it later — that's updating your .gitignore file. The order matters: remove from Git first, then ignore it going forward.

The files themselves stay on your computer. Git only forgets about them; it does not delete them from your working directory. This is the key difference between git rm (which deletes) and git rm --cached (which only stops tracking).

Key Takeaways

  • Use git rm --cached -r directory-name to stop Git from tracking a directory while keeping the files on your computer.
  • Add the directory name to your .gitignore file so Git does not re-add it in future commits.
  • Run git commit after removing to record the deletion in your repository history.
  • If the directory was already committed, other people pulling your changes will see it removed from their copies too.

Step-by-step: removing a directory Git is watching

Open your terminal or command prompt in the root of your repository — the folder where your .git folder lives. Type this command, replacing directory-name with the actual name:

git rm --cached -r directory-name

The -r flag means "recursive," so Git removes the directory and everything inside it from tracking. The --cached flag means Git forgets about it but does not delete the actual files from your disk.

Next, open your .gitignore file (it lives in the root of your repository). If it does not exist, create a new text file and name it .gitignore. Add this line:

directory-name/

The trailing slash tells Git this is a directory, not a file. Save the file. Now run:

git commit -m "Stop tracking directory-name"

This records the removal in your repository history. If you push this commit to a shared repository, other people will see the directory removed from their copies when they pull.

Why --cached instead of just rm

The --cached flag is the safety valve. Without it, git rm directory-name would delete the directory from your disk entirely — useful if you want the files gone, but not what you want here.

Think of it this way: git rm says "remove this from Git and from my computer." git rm --cached says "remove this from Git only; leave my files alone." Since you are reading this article, you probably want to keep the files.

What happens if the directory was already committed

If the directory was already in your repository history before you ran git rm --cached, Git will record its removal as a change. When you commit and push, anyone else working on the repository will see the directory disappear from their local copies when they pull your changes.

This is usually fine — it is how version control is supposed to work. But if you are working with others, tell them what you did. They may have local changes in that directory that will look like they are gone after pulling.

If you want to keep the directory in the repository history but just stop tracking it going forward, you cannot do that cleanly. Git does not have a way to say "forget about this directory from now on but keep it in the past." You either remove it (and it disappears from history) or keep tracking it.

Preventing accidental re-adds with .gitignore

After you run git rm --cached, Git stops tracking the directory. But if you later add new files to it and run git add ., Git will start tracking it again — unless you have told it to ignore it.

The .gitignore file is your insurance policy. Every line in it is a pattern Git will skip. Add the directory name with a trailing slash to ignore the whole directory:

directory-name/

If you want to ignore multiple directories, add each on its own line. If you want to ignore all directories with a certain name (like all node_modules folders anywhere in your project), you can use a pattern:

**/node_modules/

After editing .gitignore, commit it like any other file. This way, when teammates pull your changes, they will also ignore that directory.

Common mistakes and how to fix them

The most common mistake is forgetting to add the directory to .gitignore. You run git rm --cached, commit, and think you are done. Then a week later you run git add . and the directory is back in staging. Always update .gitignore first, or do it right after removing.

Another mistake is using git rm instead of git rm --cached. This deletes the directory from your disk, which you probably do not want. If you did this by accident, you can recover the files from your last commit by running git checkout HEAD -- directory-name, but it is easier to just use --cached from the start.

A third mistake is forgetting the -r flag. If you run git rm --cached directory-name without -r, Git will complain that it is a directory and refuse to remove it. Add the -r flag and try again.

When you might want to do this

You might need to stop tracking a directory if it contains files that should never have been committed — like configuration files with passwords, local build outputs, or dependency folders like node_modules. If you committed them by accident, git rm --cached is how you undo that without losing the files.

You might also do this if a directory contains files that change constantly on your machine but should not trigger commits. For example, a logs directory or a .env file. Once you stop tracking it and add it to .gitignore, Git will ignore changes to it forever.

Frequently Asked Questions

Will removing a directory from Git delete it from other people's repositories?

Yes. When you commit the removal and push it, anyone who pulls your changes will see the directory disappear from their local copy. Git is designed to keep everyone's repositories in sync. If you remove something, it gets removed for everyone.

Can I remove a directory from Git but keep it in the history?

No. Git does not have a way to say "stop tracking this from now on but keep it in the past." When you remove a directory, it is gone from all future checkouts. The history still shows it existed, but you cannot access it unless you check out an older commit.

What is the difference between .gitignore and git rm --cached?

git rm --cached removes a directory that Git is already tracking. .gitignore prevents Git from tracking new files and directories. You need both: remove what is already tracked, then ignore it going forward.

Do I need to commit after running git rm --cached?

Yes. git rm --cached stages the removal, but you still need to run git commit to record it. Until you commit, the removal only exists in your staging area and will not be shared with others.

What if I accidentally deleted the directory with git rm instead of git rm --cached?

Run git checkout HEAD -- directory-name to restore it from your last commit. If you already committed the deletion, you can restore it from an earlier commit using git checkout commit-hash -- directory-name, where commit-hash is the commit before you deleted it.