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 [options] <path>...
git check-ignore [options] --stdin

PARAMETERS

-q, --quiet
    Suppresses output for untracked paths. Only displays paths that are actually ignored.

-v, --verbose
    Shows verbose output, including the matching ignore pattern and the path to the .gitignore file (or other source) that contains the rule.

-n, --non-matching
    Shows paths that are not ignored. Useful for listing all paths provided, highlighting which ones are not affected by ignore rules.

--no-index
    Ignores the Git index when checking paths. This means it treats all paths as if they were untracked, even if they are already in the index.

--stdin
    Reads paths from standard input, one path per line, instead of from command-line arguments.

--cwd=
    Processes paths provided as if the current working directory were . Useful when running the command from a different location within a repository.

--
    Separates command-line options from the list of pathnames, allowing paths that might start with a hyphen to be correctly interpreted.

DESCRIPTION

The term "git-ignore" colloquially refers to Git's mechanism for excluding certain files or directories from version control, primarily through .gitignore files. While "git-ignore" is not a direct executable command, the git check-ignore command is its counterpart for debugging and verifying these ignore rules.

git check-ignore allows users to determine if specified paths are ignored by Git's configured ignore rules. These rules are sourced from multiple places, including the project-specific .gitignore files, the repository-local .git/info/exclude file, and a global exclude file configured via core.excludesFile.

By providing one or more file paths, git check-ignore outputs which paths are ignored, and with the --verbose option, it can show the specific pattern and source file that caused the ignore. This command is invaluable for troubleshooting when a file either is or is not being ignored as expected.

CAVEATS

The command "git-ignore" itself is not a direct executable. This analysis focuses on git check-ignore, which is the primary tool for interacting with Git's ignore mechanism.

git check-ignore does not modify ignore rules; it only reports on them. It checks if a given path is ignored, but it won't list all ignored files in a repository without providing them explicitly.

<B>UNDERSTANDING GIT'S EXCLUSION HIERARCHY</B>

Git applies ignore rules in a specific order of precedence, with later rules potentially overriding earlier ones:
1. Patterns read from the core.excludesfile configuration (global).
2. Patterns read from .git/info/exclude (repository-specific, not version controlled).
3. Patterns read from .gitignore files in the same directory as the path, or in parent directories. Rules in a .gitignore file are effective for the directory it's in and all its subdirectories.

<B><I>.GITIGNORE</I> PATTERN SYNTAX</B>

.gitignore files use patterns similar to shell globbing.
Lines starting with # are comments.
Empty lines are ignored.
A pattern ending with a / matches directories only.
A leading / anchors the pattern to the root of the .gitignore file's directory.
An exclamation mark (!) at the beginning of a pattern negates it, re-including a previously excluded file.
Asterisks (*) match any sequence of characters. Double asterisks (**) have special meaning for matching across directories.

HISTORY

The core concept of ignoring files via .gitignore existed very early in Git's development. However, git check-ignore was introduced later, in Git version 1.7.6 (released around mid-2011), specifically to address the common user difficulty in debugging why certain files were or were not being ignored. Its introduction significantly improved the clarity and diagnosability of Git's exclusion rules.

SEE ALSO

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

Copied to clipboard