LinuxCommandLibrary

git-ignore

Exclude files from Git repository tracking

TLDR

Show the content of all global and local .gitignore files

$ git ignore
copy

Ignore file(s) privately, updating .git/info/exclude file
$ git ignore [file_pattern] [[-p|--private]]
copy

Ignore file(s) locally, updating local .gitignore file
$ git ignore [file_pattern]
copy

Ignore file(s) globally, updating global .gitignore file
$ git ignore [file_pattern] [[-g|--global]]
copy

SYNOPSIS

git check-ignore [-q | --quiet] [-v | --verbose] [-n | --no-index] [--stdin] [--nul] <pathname>…

PARAMETERS

-q, --quiet
    Suppress output; exit 0 if ignored, non-zero otherwise

-v, --verbose
    Print ignore rule and source file for each ignored path

-n, --no-index
    Ignore index; check only working directory patterns

--stdin
    Read paths from standard input instead of arguments

--nul
    Use NUL instead of newline to terminate output lines

--exclude=<pattern>
    Add exclude pattern for this invocation only

DESCRIPTION

The git check-ignore command determines if specified paths are ignored by Git's ignore mechanisms, such as .gitignore files, global ignore files, or command-line exclude patterns.

It is invaluable for debugging why certain files are not tracked by Git. By default, it outputs the matching ignore rule if a path is ignored, or nothing if not. Exit status indicates: 0 for ignored, non-zero for not ignored.

Use -v or --verbose to print the exact ignore rule and source file. -q suppresses output, useful in scripts. Paths can come from command line, stdin with --stdin, and output can be NUL-terminated with --nul for safe parsing.

git check-ignore respects the index unless --no-index is used, allowing checks against working directory ignores. It simulates Git's ignore logic precisely, aiding in troubleshooting repository configurations.

Common workflow: pipe git ls-files or find outputs to verify ignores. Essential for maintainers ensuring clean repositories. (178 words)

CAVEATS

Does not modify repository; read-only diagnostic tool.
Patterns from core.excludesFile and command-line respected.
Exit codes per path when multiple; last determines overall exit.

EXAMPLE USAGE

git check-ignore -v Makefile
Outputs: .gitignore:1:*.mk Makefile

find . -name '*.tmp' | git check-ignore --stdin
Lists ignored .tmp files only.

IGNORE SOURCES

Checks in order: command-line --exclude, .git/info/exclude, core.excludesFile, .gitignore hierarchy.

HISTORY

Introduced in Git 1.7.4 (2010) to address need for ignore debugging.
Evolved with options like --stdin in 1.7.6, --nul later.
Integral to modern Git workflows for CI/CD and repo hygiene.

SEE ALSO

git status(1), git add(1), git ls-files(1), gitignore(5)

Copied to clipboard