Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
git_ignore [2018/05/27 10:52] – [Overview] rpjdaygit_ignore [2019/02/22 18:18] (current) – [Debugging gitignore files] rpjday
Line 1: Line 1:
 ===== Overview ===== ===== Overview =====
  
-How to identify untracked files that Git should intentionally ignore; files already tracked by Git are not affected.+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: Some links:
Line 8: Line 8:
   * [[https://www.gitignore.io|Generate a gitignore file]]   * [[https://www.gitignore.io|Generate a gitignore file]]
  
-===== Four levels of ignoring files =====+===== Introductory .gitignore ===== 
 + 
 +==== Basic .gitignore file ==== 
 + 
 +Excerpts from the Linux kernel source top-level ''.gitignore'' file, which forces ignoring of all these file patterns //recursively//: 
 + 
 +<code> 
 +.*                               [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 
 +... 
 +</code> 
 + 
 +==== 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: 
 + 
 +<code> 
 +
 +# Generated files 
 +
 +bin2c 
 +conmakehash 
 +kallsyms 
 +pnmtologo 
 +unifdef 
 +recordmcount 
 +sortextable 
 +asn1_compiler 
 +extract-cert 
 +sign-file 
 +insert-sys-cert 
 +</code> 
 + 
 +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): 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):
  
-  * Patterns read from the command line for those commands that support them,+  * Patterns read from the command line for those commands that support them (see ''man git-clean''),
   * Patterns read from a ''.gitignore'' file in the same directory as the path, or in any parent directory, with patterns in the higher level files (up to the toplevel of the work tree) being overridden by those in lower level files down to the directory containing the file. These patterns match relative to the location of the ''.gitignore'' file. A project normally includes such ''.gitignore'' files in its repository, containing patterns for files generated as part of the project build,   * Patterns read from a ''.gitignore'' file in the same directory as the path, or in any parent directory, with patterns in the higher level files (up to the toplevel of the work tree) being overridden by those in lower level files down to the directory containing the file. These patterns match relative to the location of the ''.gitignore'' file. A project normally includes such ''.gitignore'' files in its repository, containing patterns for files generated as part of the project build,
   * Patterns read from (repository-specific) ''$GIT_DIR/info/exclude'',   * Patterns read from (repository-specific) ''$GIT_DIR/info/exclude'',
   * Patterns read from the file specified by the configuration variable ''core.excludesFile'' (possibly ''~/.my_ignorefile'').   * Patterns read from the file specified by the configuration variable ''core.excludesFile'' (possibly ''~/.my_ignorefile'').
  
-===== Rationale for different levels of gitignore files =====+==== Rationale for different levels of gitignore files ====
  
   * Patterns which should be version-controlled and distributed to other repositories via clone (i.e., files that //all// developers will want to ignore) should go into a ''.gitignore'' file that comes with the repository.   * Patterns which should be version-controlled and distributed to other repositories via clone (i.e., files that //all// developers will want to ignore) should go into a ''.gitignore'' file that comes with the repository.
Line 23: Line 99:
   * Patterns which a user wants Git to ignore in //all// situations (e.g., backup or temporary files generated by the user’s editor of choice) generally go into a file specified by ''core.excludesFile'' in the user’s ''~/.gitconfig''.   * Patterns which a user wants Git to ignore in //all// situations (e.g., backup or temporary files generated by the user’s editor of choice) generally go into a file specified by ''core.excludesFile'' in the user’s ''~/.gitconfig''.
  
-===== Debugging gitignore files =====+<code> 
 +$ git config --global core.excludesfile ~/.my_ignorefile 
 +</code> 
 + 
 +==== Debugging gitignore files ====
  
 <code> <code>
 $ man git-check-ignore $ man git-check-ignore
 </code> </code>
 +
 +===== gitignore gotchas =====
 +
 +  * Since ''.gitignore'' is a versioned file, checking out a different commit might result in a different ''.gitignore'' file, causing unexpected behaviour.
  • git_ignore.1527418342.txt.gz
  • Last modified: 2018/05/27 10:52
  • by rpjday