title: When to use leading slash in gitignore
body: <p>I'm trying to understand more clearly the <code>.gitignore</code> syntax, and in particular as far as <a href="https://github.com/github/gitignore" rel="noreferrer">https://github.com/github/gitignore</a> gitignores are concerned.</p>
  
  <p>I see that the leading slash is used to match only pathnames relative to the location of the <code>.gitignore</code> file (from <a href="http://git-scm.com/docs/gitignore" rel="noreferrer">http://git-scm.com/docs/gitignore</a>):</p>
  
  <blockquote>
    <p>A leading slash matches the beginning of the pathname. For example, "/*.c" matches "cat-file.c" but not "mozilla-sha1/sha1.c".</p>
  </blockquote>
  
  <p>But what happens when i remove the leading slash? As far as what i understood, there are two cases:</p>
  
  <ol>
  <li>If the pattern does not contain a slash (or it contains only a trailing slash, which means that it should match a directory), the search is performed inside the entire directory tree. For example, the pattern <code>dir/</code> will match <code>&lt;root&gt;/dir</code>, <code>&lt;root&gt;/a/dir</code>, <code>&lt;root&gt;/a/b/c/.../dir</code>, etc., where <code>&lt;root&gt;</code> is the location of the <code>.gitignore</code> file.</li>
  <li>If the pattern contains a slash, which is not in the trailing position (it's not the last character), then it is matched only with pathnames relative to the <code>.gitignore</code> file location.</li>
  </ol>
  
  <p>These are the examples i made to check this behaviour:</p>
  
  <pre><code># Directory structure:
  &lt;root&gt;
  ├─ dir/
  │   └─ test
  ├─ src/
  │   ├─ dir/
  │   │   └─ test
  test file is there only because Git does not track empty directories.
  </code></pre>
  
  <p>First test:</p>
  
  <pre><code># .gitignore
  dir/
  
  # git status
  nothing to commit
  </code></pre>
  
  <p>So Git is ignoring both <code>dir</code> directories. This is consistent with case number 1: the pattern has no slashes (except for the trailing one), so Git is watching the entire directory tree, ignoring everything that matches the pattern.</p>
  
  <p>Second test:</p>
  
  <pre><code># .gitignore
  /dir/
  
  # git status
  Untracked files:
      src/
  </code></pre>
  
  <p>Here, Git is ignoring only the <code>dir</code> directory directly beneath the root directory, thanks to the leading slash in the pattern.</p>
  
  <p>Third test:</p>
  
  <pre><code># .gitignore
  dir/*
  
  # git status
  Untracked files:
      src/
  </code></pre>
  
  <p>This is consistent with the case number 2: the pattern has some slash inside it, so it is considered as a pathname starting from the root directory.</p>
  
  <p>Now it's time for the real question. Let's consider <a href="https://github.com/github/gitignore/blob/master/Magento.gitignore" rel="noreferrer">this gitignore file</a>: when they ignore the directory <code>downloader/</code>, for example, aren't they actually ignoring every single <code>downloader</code> directory found in the entire directory tree? This is what i'm driven to think since what i saw about Git's workings before.</p>
  
  <p>So if i happen to have a custom module with a <code>downloader</code> directory inside of it, will it be unexpectedly ignored as well as the regular one in the root of Magento? This is a bit of a rethorical question because it actually already happened to me, producing a really hard to find bug.</p>
  
  <p>So, in the Magento <code>.gitignore</code> file (which i'm referring to only as an example, btw) a lot of the patterns contain slashes, so they are correctly matched against pathnames starting from the root, but there are a few cases, like <code>downloader/</code> or <code>errors/</code> that, if i'm not mistaken, are potentially dangerous, and which should probably be changed to <code>/downloader/</code> and <code>/errors/</code>.</p>
  
  <p>As a more general question, should i always use the leading slash for patterns not containing slashes (except for the trailing one) when i want to select a pathname explicitly starting from root, and not to use it for patterns containing slashes, or should i always use the leading slash for clarity? What do you think about it?</p>
  
  <p>Thank you for reading and sorry for the long post.</p>