Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of leading slash in `.gitignore` file

Tags:

git

gitignore

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

like image 772
Norman Ramsey Avatar asked Jul 04 '11 16:07

Norman Ramsey


People also ask

What does double asterisk mean in Gitignore?

Double Asterisk ** can be used to match any number of directories. **/logs matches all files or directories named logs (same as the pattern logs ) **/logs/*.log matches all files ending with .log in a logs directory. logs/**/*.log matches all files ending with .log in the logs directory and any of its subdirectories.

What is exclamation mark in Gitignore?

logs/important.log. Prepending an exclamation mark to a pattern negates it. If a file matches a pattern, but also matches a negating pattern defined later in the file, it will not be ignored.

What should be in a Gitignore file?

gitignore should list the names or name-patterns of files that will be found in work-trees when working with your project, but that should not be committed to the project. In other words, it's not OS-specific, it's project-specific.


1 Answers

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.

like image 57
Paŭlo Ebermann Avatar answered Oct 06 '22 07:10

Paŭlo Ebermann