Ignoring directories in Git repositories on Windows
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Ignoring directories in Git on Windows works almost the same way it does on Linux or macOS. The main tool is still .gitignore, and the main rule is still that ignored files are only ignored if Git is not already tracking them. The Windows-specific confusion usually comes from path separators and from assuming .gitignore can retroactively untrack an existing folder.
Ignore a Directory in .gitignore
To ignore a directory, add a pattern that ends with a slash:
That tells Git to ignore those directories and their contents.
In Git ignore files, forward slashes are the normal separator, even on Windows. You do not need to write backslashes just because the machine uses Windows paths.
Ignore a Directory at a Specific Location
If you only want to ignore a folder at the repository root, start the pattern with a leading slash:
This is different from temp/, which matches any directory named temp anywhere in the repository tree.
That distinction matters in larger repositories where the same folder name may appear in several places.
Already-Tracked Directories Are the Common Surprise
A .gitignore rule does not automatically stop Git from tracking files that are already in the index. If the directory was committed earlier, you must remove it from the index once:
After that, the .gitignore rule can keep future changes in that directory out of Git.
This is the single most common reason people think their ignore rule is "not working."
A Simple Example
Suppose your repository contains a generated folder named dist. Add this to .gitignore:
If dist was never tracked, that may be all you need.
If it was tracked already, follow up with:
That combination is the real complete fix.
Project-Specific Versus Personal Ignore Rules
Not every ignore belongs in the shared repository. If the folder is only local to your machine, you may prefer one of these instead:
- '
.git/info/excludefor repository-local ignores not committed to others' - a global ignore file configured through
core.excludesfile
Example global setup:
That is useful for editor folders, OS-specific files, and other personal clutter that should not be added to every project’s shared .gitignore.
Windows-Specific Notes
Git itself still prefers forward slashes in ignore patterns, even on Windows. The shell or file explorer may show backslashes in paths, but .gitignore rules are normally written with /.
This is why a Windows rule like build\ is not the normal pattern. Use build/ instead.
Common Pitfalls
One common mistake is adding a folder to .gitignore after the folder was already committed and expecting Git to stop tracking it automatically. It will not.
Another mistake is using Windows backslashes in ignore patterns. Git ignore files normally use forward slashes.
Developers also sometimes forget the difference between temp/ and /temp/. One matches anywhere, and the other matches only at the repository root.
Finally, do not put machine-specific clutter into a project’s shared .gitignore if it only matters on your own workstation. Use local or global ignore rules instead.
Summary
- Ignore directories in Git with
.gitignorepatterns such asbuild/. - Use forward slashes in ignore rules, even on Windows.
- If the directory is already tracked, remove it from the index with
git rm --cached. - Use a leading slash when you want to target only the repository root.
- Consider local or global ignore files for Windows-specific personal clutter.
Related reading
- ImagePullBackOff local repository with Minikube
- Import existing source code to GitHub
- In a Git repository, how to properly rename a directory?
- In a Git repository, how to properly rename a directory?
- In Git, how can I recover a staged file that was reverted prior to committing?
- In Git how can you check which repo in Github you are pushing to from the command line?
- In git, how do I create a single patch for the last 2 revisions?
- In git, is there a simple way of introducing an unrelated branch to a repository?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.