.gitignore Not Working: Untrack Files Git Already Tracks

PublishedTool.gitignore Generator

Contents
  1. Why files still follow you even when the rule is correct
  2. Two commands that split the cause in two
  3. Stopping tracking: git rm --cached
  4. Pattern syntax: slashes, **, !
  5. Common Sticking Points
  6. Summary
  7. Frequently Asked Questions

You set the rules in the .gitignore at your project root correctly, yet the file keeps showing up in git status and still goes into your commits. Rewriting the rule doesn't help.

The cause isn't a typo. It's scope. Git's official documentation defines .gitignore as "a file that specifies intentionally untracked files to ignore," and it states plainly that files already tracked by Git are not affected. Once a file has gone through git add and into the index, it ignores the rules and keeps following you.

Summary ① .gitignore only applies to files that aren't tracked yet. It has no effect on files already being tracked. ② Check tracking with git ls-files --error-unmatch <path>, and check whether a rule matches with git check-ignore -v --no-index <path>. Do these separately. ③ The command that stops tracking is git rm --cached <path>. The file stays in your working folder and only leaves the index.

Why files still follow you even when the rule is correct

Git splits files into two groups: tracked files registered in the index (staging area), and untracked files that aren't registered yet.

.gitignore only deals with the second group. It's a list saying "leave these files out of the tracking candidates," not a command saying "let go of files you're already tracking."

That's why a common order causes the problem. If you create a project, run git add . first, and only then fill in .gitignore, anything that entered the index in between — node_modules/, build output, local config files — won't drop out no matter what rules you write.

This is also why the symptom shows up late. A tracked file doesn't appear in git status as long as its contents don't change. The moment you edit one line, it pops up with an M, and that's when you're confused, thinking "I clearly told it to ignore this."

Two commands that split the cause in two

Before you fix anything, separate the cause. A tracking problem and a pattern problem have completely different fixes.

Step 1: Check whether the file is tracked

git ls-files --error-unmatch config.local

If the path is printed back, it's tracked. If it's not in the index, you get the error did not match any file(s) known to git and exit code 1. To look at a whole folder, just pass the path, like git ls-files node_modules.

Step 2: Check whether a rule actually catches that path

git check-ignore -v --no-index config.local

If a rule catches it, you'll see something like .gitignore:1:config.local config.local — which file, which line, and which pattern caught it. If nothing comes out and the exit code is 1, the pattern isn't catching that path.

--no-index is the key here. By default, git check-ignore doesn't report tracked files at all, so without this flag you get empty output even when the rule is fine, and you end up chasing the wrong thing.

Combine the two results and you have your answer.

Step 1 (tracking)Step 2 (rule)Cause
PrintedMatchedTracking problem — untrack with git rm --cached below
PrintedNot matchedBoth problems — fix the pattern and untrack
Error (untracked)Not matchedPattern problem — check the syntax section

Stopping tracking: git rm --cached

Adding --cached to git rm removes the file from the index only. As the official docs put it, the file in the working tree stays exactly as it is, whether or not it's been modified. Nothing on disk gets deleted.

git rm --cached config.local          # a single file
git rm -r --cached node_modules       # a whole folder (-r = recursive)
git commit -m "chore: stop tracking ignored files"

When you run it, a D (deleted) status is staged, and it takes effect once you commit. After the commit, the file matches your .gitignore rule and goes quiet in git status too.

If you've reorganized your rules and want to apply them to the whole repository again, commit your in-progress changes first, then empty the index and refill it.

git rm -r --cached .
git add .
git status        # always check the list of files to be removed with your own eyes
git commit -m "chore: apply .gitignore"

The items marked D in git status are exactly the files that will be ignored from now on. If a file you can't afford to lose is in there, don't commit — fix .gitignore first.

Two things to watch out for. First, this only stops tracking going forward. The file stays as-is in history that's already committed, so for something like an auth key that was pushed to a shared repository, issuing a new value is the safe move.

Second, when someone else pulls, that file can disappear from their working folder. If it's a local config file each person creates for themselves, it's better to leave a note in the commit message or a team announcement saying "you'll need to recreate this on your end."

Check it with our tool. If the rule side was the problem, fill in .gitignore before you commit again. Check the presets for Node, Python, macOS, VSCode, and the like, and the standard rule sets get merged into one.

.gitignore Generator

No install or sign-up — templates are merged right in your browser, and your selections are never sent to a server.

Pattern syntax: slashes, **, !

If it was a pattern problem, looking at just three syntax rules usually solves it. All of the following are from the PATTERN FORMAT rules in Git's official documentation.

Where the slash goes determines the scope. If a slash is at the beginning or in the middle of a pattern, it becomes a relative path from the location of that .gitignore file. If there's no slash, it matches at any depth below. If there's a trailing slash, it only matches directories.

PatternWhat it catches
build/A directory named build at any depth
/build/Only the build directory at that .gitignore's location
buildBoth files and directories named build
**/foofoo at any location (same as foo)
abc/**Everything inside abc, no depth limit
a/**/ba/b, a/x/b, a/x/y/b …

! brings back what you excluded earlier. But there's one restriction, and it's the trap people hit most often. If a parent directory is excluded as a whole, you can't bring files inside it back. Git doesn't look inside excluded directories for performance reasons, so a pattern aimed at a file inside one has no effect no matter where you put it.

.vscode/                      # ✗ excludes the whole folder → the ! below is neutralized
!.vscode/settings.json

.vscode/*                     # ✓ excludes 'the items inside' → the ! works
!.vscode/settings.json

If you actually run git check-ignore -v --no-index .vscode/settings.json, the first form reports that the .vscode/ rule caught it, and the second form reports that !.vscode/settings.json caught it. You can see exactly which line won. That's also why the VSCode preset in our tool uses the .vscode/* form.

Within the same file, the last matching pattern wins. So a ! exception must come on a line below the exclusion rule. A .gitignore in a subdirectory overriding the rules in a parent directory works the same way.

Common Sticking Points

SituationWhat to check
I added a rule but the file still gets committedIt's tracked — use git rm --cached
check-ignore outputs nothingWhether you left out --no-index
A ! exception doesn't workWhether you excluded the parent directory as a whole
A file is ignored that I never listedRepo-level .git/info/exclude, global core.excludesFile
A pattern with trailing whitespaceTrailing whitespace is ignored (protect it with a backslash, not quotes)
A filename starting with # or !Put a backslash before the first character to treat it literally

The last two rows can be confusing. A line starting with # is a comment, and a line starting with ! is a negation, so to actually ignore a file that starts with that character, put a backslash before the first character, like \#temp.

The opposite situation — "it's ignored but I never added a rule" — is caught with the same command. The first column of git check-ignore -v --no-index output is the path of the file the rule came from, so you can tell right away whether the rule came from global config or from inside the repo. Check the global file's location with git config --get core.excludesFile.

Summary

  • .gitignore applies only to files that are not yet tracked. It has no effect on files that are already tracked.
  • Check tracking status with git ls-files --error-unmatch <path>, and check whether a rule matched with git check-ignore -v --no-index <path> — separately.
  • The command that stops tracking is git rm --cached (add -r for folders), and it does not delete files from your working folder. You have to commit for the change to take effect.
  • This only stops future tracking. The file stays in past history, so if credentials were exposed, it's safer to issue new ones.
  • In a pattern, the position of slashes sets the scope, and a trailing slash limits it to directories.
  • A ! exception does not work if you exclude the parent directory as a whole. Use .vscode/* instead of .vscode/.
.gitignore Generator

No install or sign-up — templates are merged right in your browser, and your selections are never sent to a server.

Frequently Asked Questions

Does git rm --cached actually delete the file?

No. The official documentation explains that this option removes paths from the index only, and leaves files in the working tree untouched whether or not they have been modified.

The file stays on disk; it's just that after you commit, Git no longer follows it. However, the file may disappear from the working folder of anyone else who receives that deletion commit.

If I ignore a config file I already committed, does it disappear from history too?

No. Adding it to .gitignore and running git rm --cached only stops future tracking. Past commits still contain the contents exactly as they were, and anyone who clones the repo can pull the file out of the history.

Erasing the history itself requires separate work to rewrite commits, and if you're collaborating, the impact is significant. If a value was exposed — like an auth key — issuing a new value is the reliable fix, before you touch the history.

Does a .gitignore in a subfolder apply too?

Yes. Git checks the .gitignore in the directory containing the path and in its parent directories, and rules in the lower-level file override rules in the higher-level file.

After that, it checks repo-level .git/info/exclude, and last the global file set by core.excludesFile. You can see which file and which line won for a given file in the output of git check-ignore -v --no-index.

Are the items I select in the generator sent to a server?

No. The preset templates are contained in the page, so selecting, combining, and copying all happen in your browser. If you pick several presets together you'll get overlapping lines like build/, and with 'Remove duplicate lines' turned on, each duplicate line is kept only once.

This article was adapted from our Korean original. Read the Korean original

Related articles

  1. SHA-256 Hash Generator: How to Verify Data IntegritySee what SHA-256 actually computes, why a one-character change flips the whole hash, and how to check a text hash in your browser.
  2. HTML Email Signature Images Not Showing: Why and How to FixLearn why logos and photos in HTML email signatures get blocked or fail to load, and how to build a signature that displays safely.
  3. Invoice vs. Quote: When to Send Each OneAn estimate comes before the contract, an invoice after the work. See how issue timing separates the two, what each document needs, and how to total tax.
  4. JSON-LD structured data: where to put it and required propertiesWhere to put JSON-LD in your HTML, which properties are required vs. recommended per Google, and why valid markup still may not show rich results.
  5. Lotto 6/45 Odds and Number Generator: Lock and Exclude NumbersLearn how Lotto 6/45 odds work (8,145,060 combinations), how locked and excluded numbers shrink them, and how Fisher-Yates shuffling picks numbers fairly.
  6. What Is a MAC Address? Random MACs and Wi-Fi TrackingA MAC address is a fixed 48-bit hardware ID, which makes it easy to track a device across Wi-Fi networks. Here's how random MAC addresses break that link.

Related tools