Difference between .gitignore rules with and without trailing slash like /dir and /dir/

后端 未结 3 1983
隐瞒了意图╮
隐瞒了意图╮ 2021-01-29 23:15

Is there a difference between /dir and /dir/ in the .gitignore file within a Git repository?

How are the following different?

/         


        
3条回答
  •  清歌不尽
    2021-01-30 00:08

    Git 2.23 (Q3 2019) attempts to revamp the description about slashes in gitignore patterns (used to indicate things like "anchored to this level only" and "only matches directories")

    The documentation now includes:

    • The slash '/' is used as the directory separator.
      Separators may occur at the beginning, middle or end of the .gitignore search pattern.

    • If there is a separator at the beginning or middle (or both) of the pattern, then the pattern is relative to the directory level of the particular .gitignore file itself.
      Otherwise the pattern may also match at any level below the .gitignore level.

    • If there is a separator at the end of the pattern then the pattern will only match directories, otherwise the pattern can match both files and directories.

    • For example, a pattern doc/frotz/ matches doc/frotz directory, but not a/doc/frotz directory;
      however frotz/ matches frotz and a/frotz that is a directory (all paths are relative from the .gitignore file).

    • An asterisk "*" matches anything except a slash.
      The character "?" matches any one character except "/".
      The range notation, e.g. [a-zA-Z], can be used to match one of the characters in a range.
      See fnmatch(3) and the FNM_PATHNAME flag for a more detailed description.

提交回复
热议问题