Yanyg - SAN Software Engineer

Git - gitignore配置

目录

Empty

1 gitignore介绍

我使用git管理代码和文本文档,例如,emacs org repo。一些文档类型是中间产物,希望 git不提示untracked,错误的添加此类文档时也希望能忽略并弹出提示。这是gitignore的功能。Gitbook 2.2 Git Basics - Recording Changes to the Repository有详细介绍。

1.1 gitignore语法规则

文档描述gitignore语法规则有五条:

  1. 忽略空白行或以#开始的行(注释);
  2. 按照标准glob工作,并递归应用于整个工作目录(参考glob);
  3. 模式以/打头,禁止递归工作;
  4. 模式以/结尾,限定特定目录;
  5. 使用感叹号!对模式含义取反;

gitignore规则细化补充如下:

  • 通用的规则应该写在前面,专有的放后面。比如,希望忽略*.a,但不要忽略lib.a:
*.a
!lib.a
  • 规则中不含有/的,则当作通配符处理,每个目录迭代检查;
# ignore all xyz.txt
xyz.txt

# only ignore xyz.txt in repo root directory
/xyz.txt
  • 规则以/结尾的,仅限于目录,而不包含同名的链接和文件;
  • 模式以连续两个星号和/打头(**/)时,和移除**/含义相同;
  • 模式以连续两个型号结尾时,表示匹配其下一切内容,含子目录;
  • 模式中间包含/**/时,**匹配0个或多个目录;比如:
# match x/y/b, x/w/b, or x/y/z/b
x/**/b

更多细节参考man

1.2 gitignore文件位置

  • $HOME/.config/git/ignore
  • git core.excludeFile指定名称
  • git仓库内任意目录下的.gitignore,只对当前目录(及当前目录的子目录)生效

2 glob介绍

glob是shell下使用的通配模式,WIKI下有较为细节描述。简单描述如下:

*
匹配零个或多个任意字符
?
匹配单个任意字符
[abc]
匹配[]中的任意一个字符
[a-z0-9]
匹配范围内的任意字符,比如:a到z,或0-9任意字符
[!abc]
匹配不在[]中的任意字符
[!a-z]
匹配范围之外的任意字符

3 gitignore(org repo示例)

3.1 需求

  • 包含所有的.org文件
  • 包含css目录css文件,r目录全部文件,code目录全部c,……
  • 忽略其他文件
  • 根据需要动态定制增强

3.2 实现

# default ignore all
/**/*.*

# do not ignore all org files
!/**/*.org
!/**/css/*.css
!/**/code/*.c
!/**/fonts/*.ttf
!/**/r/*.*
!/**/img/*.*
!/**/.gitignore

# ignore all .#*
/**/.#*
/**/*.*~

3.3 其他

参考github ignore

4 粘贴内容

4.1 Git Basics - Recording Changes to the Repository (Copied)

URL
https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository

Often, you’ll have a class of files that you don’t want Git to automatically add or even show you as being untracked. These are generally automatically generated files such as log files or files produced by your build system. In such cases, you can create a file listing patterns to match them named .gitignore. Here is an example .gitignore file:

$ cat .gitignore
*.[oa]
*~

The first line tells Git to ignore any files ending in “.o” or “.a” — object and archive files that may be the product of building your code. The second line tells Git to ignore all files whose names end with a tilde (~), which is used by many text editors such as Emacs to mark temporary files. You may also include a log, tmp, or pid directory; automatically generated documentation; and so on. Setting up a .gitignore file for your new repository before you get going is generally a good idea so you don’t accidentally commit files that you really don’t want in your Git repository.

The rules for the patterns you can put in the .gitignore file are as follows:

  • Blank lines or lines starting with # are ignored.
  • Standard glob patterns work, and will be applied recursively throughout the entire working tree.
  • You can start patterns with a forward slash (/) to avoid recursivity.
  • You can end patterns with a forward slash (/) to specify a directory.
  • You can negate a pattern by starting it with an exclamation point (!).

Glob patterns are like simplified regular expressions that shells use. An asterisk (*) matches zero or more characters; [abc] matches any character inside the brackets (in this case a, b, or c); a question mark (?) matches a single character; and brackets enclosing characters separated by a hyphen ([0-9]) matches any character between them (in this case 0 through 9). You can also use two asterisks to match nested directories; a/**/z would match a/z, a/b/z, a/b/c/z, and so on.

Here is another example .gitignore file:

# ignore all .a files
*.a

# but do track lib.a, even though you're ignoring .a files above
!lib.a

# only ignore the TODO file in the current directory, not subdir/TODO
/TODO

# ignore all files in the build/ directory
build/

# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt

# ignore all .pdf files in the doc/ directory and any of its subdirectories
doc/**/*.pdf

4.2 gitignore man

GITIGNORE(5) – 09/25/2017 – Git 2.11.0 – Git Manual

NAME gitignore - Specifies intentionally untracked files to ignore

SYNOPSIS

$HOME/.config/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):

  • Patterns read from the command line for those

commands that support them.

  • 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 $GIT_DIR/info/exclude.
  • Patterns read from the file specified by the

configuration variable core.excludesFile.

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

  • 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.

  • Patterns which are specific to a particular

repository but which do not need to be shared with other related repositories (e.g., auxiliary files that live inside the repository but are specific to one user’s workflow) should go into the $GIT_DIR/info/exclude file.

  • 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. Its default value is $XDG_CONFIG_HOME/git/ignore. If $XDG_CONFIG_HOME is either not set or empty, $HOME/.config/git/ignore is used instead.

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

  • A blank line matches no files, so it can serve as a

separator for readability.

  • A line starting with # serves as a comment. Put a

backslash ("\") in front of the first hash for patterns that begin with a hash.

  • Trailing spaces are ignored unless they are quoted

with backslash ("\").

  • An optional prefix "!" which negates the pattern;

any matching file excluded by a previous pattern will become included again. It is not possible to re-include a file if a parent directory of that file is excluded. Git doesn’t list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined. Put a backslash ("\") in front of the first "!" for patterns that begin with a literal "!", for example, "\!important!.txt".

  • If the pattern ends with a slash, it is removed for

the purpose of the following description, but it would only find a match with a directory. In other words, foo/ will match a directory foo and paths underneath it, but will not match a regular file or a symbolic link foo (this is consistent with the way how pathspec works in general in Git).

  • If the pattern does not contain a slash /, Git

treats it as a shell glob pattern and checks for a match against the pathname relative to the location of the .gitignore file (relative to the toplevel of the work tree if not from a .gitignore file).

  • Otherwise, Git treats the pattern as a shell glob

suitable for consumption by fnmatch(3) with the FNM_PATHNAME flag: wildcards in the pattern will not match a / in the pathname. For example, "Documentation/*.html" matches "Documentation/git.html" but not "Documentation/ppc/ppc.html" or "tools/perf/Documentation/perf.html".

  • A leading slash matches the beginning of the

pathname. For example, "/*.c" matches "cat-file.c" but not "mozilla-sha1/sha1.c".

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

  • A leading "**" followed by a slash means match in

all directories. For example, "**/foo" matches file or directory "foo" anywhere, the same as pattern "foo". "**/foo/bar" matches file or directory "bar" anywhere that is directly under directory "foo".

  • A trailing "/**" matches everything inside. For

example, "abc/**" matches all files inside directory "abc", relative to the location of the .gitignore file, with infinite depth.

  • A slash followed by two consecutive asterisks then a

slash matches zero or more directories. For example, "a/**/b" matches "a/b", "a/x/b", "a/x/y/b" and so on.

  • Other consecutive asterisks are considered invalid.

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.

EXAMPLES

$ git status […]

[…]

[…] $ cat .git/info/exclude

*.[oa] $ cat Documentation/.gitignore

*.html

!foo.html $ git status […]

[…]

[…]

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

/* !/foo /foo/* !/foo/bar

SEE ALSO

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

GIT

Part of the git(1) suite

5 参考资料