Sub topic 11
Listing Ignored Files and Forcing Them In
This chapter assumes you can write .gitignore patterns and know the basic git add, git commit, and git status workflow. Here you will see which files Git is ignoring and how to track one of them anyway.
Why ignored files are hidden
By default, git status does not mention ignored files. That keeps the output short, but it can hide problems. A pattern that is too broad may be ignoring a file you wanted to commit. You also may want to check what a build left behind. Git has options for both cases.
Listing ignored files with git status
Add the --ignored flag to see them:
git status --ignoredThe output gains a section like this:
Ignored files:
(use "git add -f <file>..." to include in what will be committed)
build/
debug.logGit collapses a fully ignored directory into one entry, such as build/. The short format is easier to scan. Ignored paths start with !!:
git status --short --ignored?? notes.txt
!! build/
!! debug.logThe ?? prefix marks untracked files and !! marks ignored files.
Other ways to list them
Two other commands answer related questions:
# List every ignored file individually, not collapsed into directories
git ls-files --others --ignored --exclude-standard
# Preview which ignored files git clean would delete (nothing is removed)
git clean -ndXThe --exclude-standard option tells ls-files to use all the normal ignore sources: .gitignore files, .git/info/exclude, and the global excludes file. In git clean, -n is a dry run, -d includes directories, and -X limits the command to ignored files. Always run it with -n first, because git clean -fdX deletes ignored files permanently.
Forcing an ignored file in
Suppose *.log is ignored, but you want to commit build/special.log. A plain git add refuses:
git add build/special.logThe following paths are ignored by one of your .gitignore files:
build/special.log
hint: Use -f if you really want to add them.The -f (force) flag overrides the rules for that one path:
git add -f build/special.log
git commit -m "Add special.log as a reference fixture"What happens after forcing
Ignore rules only decide which untracked files Git offers to track. Once special.log is in the index, it is a tracked file, and Git reports its changes like any other. The rule that matched it no longer applies to it. The commit you made also stays valid if you later edit .gitignore.
Pitfalls
Forcing a directory adds more than you may expect. This command stages every ignored file under build/, not only the one you had in mind:
git add -f build/Name the exact file instead. Run git status before you commit to check what is staged.
Force-adding also only helps the person who runs it. A teammate who creates a new matching file will still find it ignored, because the rule is unchanged. If a whole category of files should be tracked, change the rules instead. Use a negation such as !important.log, which you saw in the negation chapter.
Summary of commands
| Goal | Command |
|---|---|
| Show ignored files in status | `git status --ignored` |
| Compact view with `!!` markers | `git status --short --ignored` |
| List each ignored file | `git ls-files --others --ignored --exclude-standard` |
| Preview deleting ignored files | `git clean -ndX` |
| Track one ignored file | `git add -f <file>` |
Exercise
Your .gitignore contains "*.log". You run "git add -f reports/audit.log" and commit. A week later you edit audit.log. Does "git status" show the change, and why?
Show answer
Yes. Forcing the add made audit.log a tracked file. Ignore rules only affect untracked files, so the "*.log" pattern no longer hides modifications to it.
Exercise
Which command lists ignored files one by one, instead of collapsing a fully ignored directory into a single entry?
Show answer
git ls-files --others --ignored --exclude-standard