gitignore - Man Page

Specifies intentionally untracked files to ignore

Synopsis

$XDG_CONFIG_HOME/git/ignore, $GIT_DIR/info/exclude, .gitignore

Description

A gitignore file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected; see the Notes below for details.

Each line in a gitignore file specifies a pattern. When deciding whether to ignore a path, Git normally checks gitignore patterns from multiple sources, with the following order of precedence, from highest to lowest (within one level of precedence, the last matching pattern decides the outcome):

Which file to place a pattern in depends on how the pattern is meant to be used.

The underlying Git plumbing tools, such as git ls-files and git read-tree, read gitignore patterns specified by command-line options, or from files specified by command-line options. Higher-level Git tools, such as git status and git add, use patterns from the sources specified above.

Pattern Format

Two consecutive asterisks ("**") in patterns matched against full pathname may have special meaning:

Configuration

The optional configuration variable core.excludesFile indicates a path to a file containing patterns of file names to exclude, similar to $GIT_DIR/info/exclude. Patterns in the exclude file are used in addition to those in $GIT_DIR/info/exclude.

Notes

The purpose of gitignore files is to ensure that certain files not tracked by Git remain untracked.

To stop tracking a file that is currently tracked, use git rm --cached to remove the file from the index. The filename can then be added to the .gitignore file to stop the file from being reintroduced in later commits.

Git does not follow symbolic links when accessing a .gitignore file in the working tree. This keeps behavior consistent when the file is accessed from the index or a tree versus from the filesystem.

Examples

    $ git status
    [...]
    # Untracked files:
    [...]
    #       Documentation/foo.html
    #       Documentation/gitignore.html
    #       file.o
    #       lib.a
    #       src/internal.o
    [...]
    $ cat .git/info/exclude
    # ignore objects and archives, anywhere in the tree.
    *.[oa]
    $ cat Documentation/.gitignore
    # ignore generated html files,
    *.html
    # except foo.html which is maintained by hand
    !foo.html
    $ git status
    [...]
    # Untracked files:
    [...]
    #       Documentation/foo.html
    [...]

Another example:

    $ cat .gitignore
    vmlinux*
    $ ls arch/foo/kernel/vm*
    arch/foo/kernel/vmlinux.lds.S
    $ echo '!/vmlinux*' >arch/foo/kernel/.gitignore

The second .gitignore prevents Git from ignoring arch/foo/kernel/vmlinux.lds.S.

Example to exclude everything except a specific directory foo/bar (note the /* - without the slash, the wildcard would also exclude everything within foo/bar):

    $ cat .gitignore
    # exclude everything except directory foo/bar
    /*
    !/foo
    /foo/*
    !/foo/bar

See Also

git-rm(1), gitrepository-layout(5), git-check-ignore(1)

Git

Part of the git(1) suite

Referenced By

git(1), gitattributes(5), git-check-ignore(1), git-clean(1), git-commit(1), git-commit-tree(1), git-config(1), git-crypt(1), gitfaq(7), git-init(1), git-lfs-checkout(1), git-lfs-clone(1), git-lfs-config(5), git-lfs-faq(7), git-lfs-fetch(1), git-lfs-filter-process(1), git-lfs-fsck(1), git-lfs-migrate(1), git-lfs-prune(1), git-lfs-pull(1), git-lfs-smudge(1), git-lfs-track(1), git-log(1), git-ls-files(1), git-read-tree(1), gitrepository-layout(5), git-show(1), git-sparse-checkout(1), git-status(1), tree(1).

02/23/2024 Git 2.44.0 Git Manual