Meaning of leading slash in `.gitignore` file

2020-05-19 04:40发布

问题:

If I put a pattern in a .gitignore file with a leading slash, does the slash refer to the directory in which the .gitignore file is located, or does it refer to the root of the whole repository?

(The man pages I have found have hidden this information carefully.)

回答1:

This is the documentation text:

  • A leading slash matches the beginning of the pathname. For example, "/*.c" matches "cat-file.c" but not "mozilla-sha1/sha1.c".

These are my guesses:

  • If you use an in-repository-.gitignore, the directory in which the .gitignore is located - it is not really useful to make it relative to the repository root.

    A short experiment with same-named files in the repository root and a subdirectory (and a .gitignore in this same directory) confirms this.

  • If you use an .git/info/exclude or core.excludesfile, I suppose it is relative to the repository root.

    Also this is (for info/exclude) confirmed by the same test (both tests on 1.7.3.4).

Schnouki found the right part of the documentation about this (emphasis mine):

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

One could say that this fact could have been repeated again at the later part quoted above, for clarity.



标签: git gitignore