Elye
Elye

Reputation: 60211

Ignoring folder in .gitignore, what's the different when we put `/` and not putting it?

In .gitignore, we can ignore a folder using as below

Folder
/Folder
Folder/

Is there any difference between the 3 approaches above? i.e.

  1. not putting any /
  2. putting / in front
  3. putting / behind

Upvotes: 0

Views: 59

Answers (2)

torek
torek

Reputation: 489083

John Zwink's answer covers the question you asked, but not ones you didn't ask, such as: What about /Folder/?

To cover all cases, we must make a few preliminary notes:

  • You can put a .gitignore into your working tree at the top level, or at any sub-level. That is, you can have:

    .gitignore
    dir1/.gitignore
    dir1/dir2/.gitignore
    dir1/dir2/file2
    dir1/file1
    file3
    

    for instance.

  • You can put paths into any of these .gitignore files. For instance, the top level .gitignore can list dir1/dir2/file2.

  • Any .gitignore can have a negated entry, that starts with !; the top level .gitignore could list !dir1/dir2/file2 instead.

Next, we must note that an entry in a .gitignore can begin with a slash, contain a slash, and/or end with a slash. That is, any of those .gitignore entries could list any or all of the following:

  • /f
  • /f/
  • f
  • f/
  • /d/f
  • /d/f/
  • d/f
  • d/f/

plus all of the same but with ! at the front.

This makes sixteen (16) possible combinations: prefix-slash, embedded-slash, and trailing-slash, with or without prefix-!. Fortunately for us, there are only eight (8) actual behaviors, resulting from three (3) combinations that we can mix and match any way we like:

  • Git can treat the entry you put in as a folder (directory) name only, or as a folder-or-file name.
  • Git can treat the entry as anchored or unanchored.
  • Git can treat the entry as negated ("don't ignore") or regular ("do ignore").

To mark an entry as folder/directory only, the rule is: add a trailing slash /.

To mark an entry as anchored, the rule is: add a leading /, or include an embedded / that isn't at the end. (The one at the end is already in use, as the "directory/folder mark".)

To mark an entry as negated, the rule is: add a leading ! (before any leading /).

Hence, for example, !d/f matches any directory or file named d/f, but is "anchored", and in the end, is negated as well. So we won't ignore d/f.

I borrow the term anchored from regular expressions. It means: only match at this level. That is, given a working tree that contains dir1/dir2/file2, if we tell Git to ignore file2, it will ignore this file2 even though it's deep in dir1/dir2. But if we tell Git to ignore /file2, it will only ignore a file2 that's at the same level as the .gitignore itself. If that's dir1/.gitignore, this ignores dir1/file2, but not dir1/dir2/file2.

There are two more things you need to be aware of here:

  • "Ignore" doesn't mean what a lot of people think it means.
  • If you let Git ignore a directory, Git never gets around to looking inside the directory. This makes it impossible to "un-ignore" stuff inside the directory.

Both of these trip people up, so be aware of them. They have nothing to do with the syntax of the entries themselves, though, so I won't cover them here.

Upvotes: 1

John Zwinck
John Zwinck

Reputation: 249394

Folder ignores a file or directory anywhere in the tree.

/Folder ignores a file or directory at the root of the tree.

Folder/ ignores a directory anywhere in the tree.

Upvotes: 3

Related Questions