Meaning of leading slash in `.gitignore` file

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

  • How to mirror Github and VS Online?
  • git commit broken time zone
  • What is the advantage of using git submodules over having a repo inside another repo with a gitignore?
  • gitolite installation issue
  • Restoring gitolite backup not working
  • `git log -p path/to/file` doesn't show all the changes
  • is it safe to interrupt writing objects while pushing to heroku git repo?
  • Git doesn't see new files, only the folder
  • How to make a composer depency commitable?
  • Git log ^ algorithm
  • Reset local repository branch to be just like remote repository HEAD
  • Heroku - Git push without building
  • One Solution collect form web for “Meaning of leading slash in `.gitignore` file”

    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 Baby is a git and github fan, let's start git clone.