git忽略版本库中的文件——.gitignore

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/brink_compiling/article/details/81355208

背景

有些时候,工作目录中的一些文件没有必要提交到版本库中,如ide生成元数据文件、程序编译或解释产生的中间文件、源数据、密码配置文件等等。
为了解决这个问题,可以在git工作区的根目录下创建一个特殊的.gitignore文件,将要忽略的文件写进去,git就会忽略对这些文件的版本控制。

.gitignore语法

GitHub上有一个开源库(https://github.com/github/gitignore),包含了常见的编程语言在不同的开发背景下所需要忽略的文件模板。我们可以在此基础上增加自己的配置进行使用。
.gitignore的一般语法如下:

  • 使用标准的 glob 模式匹配(支持*, ?, []等)。
  • #开头注释,会被 Git 忽略。
  • 以(/)开头防止递归。
  • 以(/)结尾指定目录。
  • 要忽略指定模式以外的文件或目录,可以在模式前加上惊叹号(!)取反。

示例:

# no .a files
*.a

# but do track lib.a, even though you're ignoring *.a files above
!lib.a

# only ignore the TODO file in the current directory, not subdir/TODO
/TODO

# ignore all files in the build/ directory
build/

# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt

# ignore all .pdf files in the doc/ directory
doc/**/*.pdf

猜你喜欢

转载自blog.csdn.net/brink_compiling/article/details/81355208