Skip to main content

How to Ignore a Folder in Git with .gitignore

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
/<folder>/
PartWhat it means
/ at the startOnly the folder next to the .gitignore file, not one with the same name deeper down
<folder>The folder's name
/ at the endFolders 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 --version to see which one you have; this guide uses Git 2.56.1.

Step 1: See the Folder Git Wants to Add​

Terminal
git status --short
Output
?? app.py
?? cache/

The terminal after git status --short: two untracked entries, app.py and the cache folder.

?? 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:

.gitignore
/cache/

.gitignore in a macOS-style code window, with the single line /cache/.

Step 3: Check That Git Ignores It​

Terminal
git status --short
Output
?? .gitignore
?? app.py

The terminal after git status --short: .gitignore and app.py are listed, and the cache folder is gone.

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:

Terminal
git check-ignore -v cache/thumb-001.png
Output
.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:

Output
?? .gitignore
?? app.py
?? src/cache/notes.txt

With cache/, without the leading slash, the same git status --short --untracked-files=all hides it too:

Output
?? .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:

Output
M cache/thumb-001.png
?? .gitignore

git rm -r --cached cache stops tracking the folder without deleting it from your disk:

Output
rm 'cache/thumb-001.png'
Output
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​

MistakeWhat happensDo this instead
Adding a folder to .gitignore after committing itGit keeps tracking it and shows its changesRun git rm -r --cached <folder> and commit
Writing cache/ when you mean only the top folderEvery folder named cache, at any depth, is ignoredWrite /cache/

Troubleshooting​

SymptomLikely causeFix
A folder in .gitignore still shows changesIt was committed before the rule was addedgit rm -r --cached <folder>, then commit
A folder deeper in the project isn't ignoredThe 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.

Additional Resources​

  • gitignore — every pattern rule.
  • git rm — --cached and its other options.