Table of Contents

Overview

How to identify untracked files that Git should intentionally ignore when running numerous commands, such as git add -u, git status, git clean and many more.

Some links:

Introductory .gitignore

Basic .gitignore file

Excerpts from the Linux kernel source top-level .gitignore file, which forces ignoring of all these file patterns recursively:

.*                               [all hidden files/directories]
*.a
*.asn1.[ch]
*.bz2
*.c.[012]*.*
*.dt.yaml
*.dtb
*.dtb.S
*.dwo
*.elf
*.gz
*.ko
...
Module.symvers                    [specific filename]
...
#
# Top-level generic files
#
/tags
/TAGS
/linux
/vmlinux
/vmlinux.32
/vmlinux-gdb.py
...
/debian/                           [top-level directory only]
...
#
# Exceptions: git files that we don't want to ignore even if they are dot-files
#
!.gitignore
!.mailmap
!.cocciconfig
!.clang-format
...
#
# Generated include files
#
include/config
include/generated
include/ksym
arch/*/include/generated
...

Recursive .gitignore

Subdirectories can have more specific .gitignore files that take precedence in those subdirectories. For the Linux kernel source, the top-level directory scripts/ has the .gitignore file:

#
# Generated files
#
bin2c
conmakehash
kallsyms
pnmtologo
unifdef
recordmcount
sortextable
asn1_compiler
extract-cert
sign-file
insert-sys-cert

The Linux kernel source repo has over 200 .gitignore files.

Advanced ignoring

Four levels of ignoring files

Each line in a gitignore/exclude 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):

Rationale for different levels of gitignore files

$ git config --global core.excludesfile ~/.my_ignorefile

Debugging gitignore files

$ man git-check-ignore

gitignore gotchas