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

后端 未结 3 1831
隐瞒了意图╮
隐瞒了意图╮ 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:06

    The Patterns Have Different Meanings

    According to the Pattern Format section of gitignore(5):

    • If the pattern ends with a slash, it is removed for the purpose of the following description, but it would only find a match with a directory. In other words, foo/ will match a directory foo and paths underneath it, but will not match a regular file or a symbolic link foo (this is consistent with the way how pathspec works in general in git).

    • If the pattern does not contain a slash /, git treats it as a shell glob pattern and checks for a match against the pathname relative to the location of the .gitignore file (relative to the toplevel of the work tree if not from a .gitignore file).

    What this means is that "dir" can be a file, directory, or symbolic link, but "dir/" with a trailing slash will only match a directory. In most cases, the difference won't matter, but when it does, understanding the distinction can remove ambiguity from your .gitignore files.

提交回复
热议问题