How to Ignore a Folder in Git with .gitignore
Some folders don't belong in Git: caches, downloaded data, build output, trained models. One line in a .gitignore file keeps a whole folder out, such as /cache/. This guide ignores a folder, shows what the two slashes change, and fixes a folder Git was already tracking, with Git 2.56.1.
Syntax
/<folder>/
| Part | What it means |
|---|---|
/ at the start | Only the folder next to the .gitignore file, not one with the same name deeper down |
<folder> | The folder's name |
/ at the end | Folders only, not files with that name |
Git's gitignore documentation gives both rules. For the start: "If there is a separator at the beginning or middle (or both) of the pattern, then the pattern is relative to the directory level of the particular .gitignore file itself." For the end: "If there is a separator at the end of the pattern then the pattern will only match directories".
The Example
A small photo app has a script, app.py, and a cache folder of thumbnails it can always create again. The folder should stay out of the repository.
Prerequisites
- Git, in a repository. Run
git --versionto see which one you have; this guide uses Git 2.56.1.
Step 1: See the Folder Git Wants to Add
git status --short
?? app.py
?? cache/

?? means untracked. cache/ is waiting to be added along with the script.
Step 2: Add the Folder to .gitignore
Create a file called .gitignore in the repository's top folder, with this line:
/cache/

Step 3: Check That Git Ignores It
git status --short
?? .gitignore
?? app.py

cache/ is gone from the list, and .gitignore itself appears: commit it, so the rule applies everywhere the project is cloned. To see which line matched, ask Git about a file in the folder:
git check-ignore -v cache/thumb-001.png
.gitignore:1:/cache/ cache/thumb-001.png
Line 1 of .gitignore, /cache/, ignores it. The commands work unchanged in PowerShell.
With or Without the Leading Slash
Add another folder called cache deeper in the project, src/cache/notes.txt. With /cache/, Git still shows it:
?? .gitignore
?? app.py
?? src/cache/notes.txt
With cache/, without the leading slash, the same git status --short --untracked-files=all hides it too:
?? .gitignore
?? app.py
Use /cache/ for one folder at the top, and cache/ for every folder of that name.
A Folder Git Already Tracks
.gitignore only affects files Git doesn't track yet. The documentation says: "Files already tracked by Git are not affected". In a copy of the app where cache/ had already been committed, adding /cache/ and then changing a thumbnail still showed it:
M cache/thumb-001.png
?? .gitignore
git rm -r --cached cache stops tracking the folder without deleting it from your disk:
rm 'cache/thumb-001.png'
D cache/thumb-001.png
?? .gitignore
D means the next commit removes the file from the repository; it's still in the cache folder on your computer. The git rm documentation describes --cached: "Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left alone."
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Adding a folder to .gitignore after committing it | Git keeps tracking it and shows its changes | Run git rm -r --cached <folder> and commit |
Writing cache/ when you mean only the top folder | Every folder named cache, at any depth, is ignored | Write /cache/ |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
A folder in .gitignore still shows changes | It was committed before the rule was added | git rm -r --cached <folder>, then commit |
| A folder deeper in the project isn't ignored | The pattern starts with / | Drop the leading slash, or add a second line for that path |
FAQ
Should .gitignore itself be committed?
Yes, so everyone who clones the project gets the same rules.
Does ignoring a folder delete it?
No. Ignored files stay on your disk; Git just doesn't track them.
Conclusion
/cache/ in .gitignore took the folder out of git status, while a deeper cache folder still showed until the leading slash came off. For a folder Git already tracked, git rm -r --cached stopped tracking it and left the files where they were.
