Course outline

Git and GitHub / Basic: the local Git model / Lesson 7

Ignore patterns and tracked files

Explain and diagnose Git ignore rules while distinguishing untracked exclusions from files already in the index.

About 3 min readContent reviewed
On this page

Lesson goals

  • Read and test basic ignore patterns.
  • Explain why tracked files remain tracked.
  • Inspect the matching rule before changing repository state.

Understand what ignore means

Ignore rules tell Git which intentionally untracked paths to omit from ordinary untracked-file discovery. They are not a security boundary and do not remove content already tracked or committed. Decide whether a rule belongs in the shared repository, a local exclusion or another documented source. Never put credentials in a repository merely because an ignore pattern appears to match them.

Define a small rule set

In a disposable lab repository, this root .gitignore ignores files ending in .log and then negates the rule for keep.log. Pattern scope and directory rules matter; a negated child cannot be re-included if its parent directory is excluded. The example is illustrative and should not replace a project’s existing ignore policy.

gitignore Synthetic lab ignore file
*.log
!keep.log
Expected output
debug.log is ignored; keep.log is not ignored by these rules.

The later negation exempts keep.log from the preceding log-file rule.

Inspect the rule

Run these commands only inside the isolated lab after creating the synthetic files. Status can show ignored paths, while check-ignore with verbose output identifies the source and pattern. A negated match can still be reported by verbose diagnostics; inspect the leading exclamation mark rather than assuming every printed path is ignored. The exact output includes the matching file and line.

bash Commands for a disposable repository
git status --short --ignored
git check-ignore -v -- debug.log
git check-ignore -v -- keep.log
Expected output
Verbose output identifies the applicable .gitignore rules.

The commands inspect status and the matching rule without adding or removing tracked files.

Handle tracked paths deliberately

A path already in the index continues to be tracked even if a new ignore rule matches its name. Inspect git status and the index before deciding what action is appropriate. Removing a tracked file from the index changes the next commit and can affect collaborators; review that change and use the project’s normal process. A rule also cannot erase earlier commits or copies.

Verify the policy

Test an ignored file, an intended exception and a previously tracked file in the lab. Inspect both the rule source and staged diff before committing any shared policy. Check that build outputs remain reproducible and that required project files were not hidden. If a secret was committed, follow the owner’s incident and credential rotation process rather than relying on a later ignore rule.

Common errors and fixes

A tracked log file still appears as modified.

Why it happens
Ignore rules do not stop tracking an indexed path.
How to fix it
Review the tracked file and decide the appropriate version-control change.
Prevent it next time
Inspect the index before adding ignore rules.

An exception inside an ignored directory does not appear.

Why it happens
The excluded parent prevents Git from traversing to the child.
How to fix it
Adjust the parent and child patterns deliberately.
Prevent it next time
Test the actual directory hierarchy.

A secret remains exposed in history.

Why it happens
An ignore rule was mistaken for removal from existing commits.
How to fix it
Follow the owner’s incident and rotation process.
Prevent it next time
Keep credentials out of commits and inspect staged changes.

2 practice exercises

1. Does adding *.log to .gitignore stop tracking an already committed log?

Show worked solution

No. The path remains tracked until an explicitly reviewed index change removes it, and the old commits still contain it.

2. Why inspect the exclamation mark in verbose check-ignore output?

Show worked solution

Verbose diagnostics can show a negating rule as the matching pattern. The printed path alone does not mean the final rule ignores it.

Key takeaways

  • Ignore rules target intentionally untracked paths.
  • Inspect rule sources and negations in a lab.
  • Ignore rules do not remove secrets or history.

Official references