在写程序时,总会有一些编译产生的临时文件或者测试时产生的临时文件,在GIT版本控制系统中,可以使用编辑.gitingore文件过滤掉这些无用文件。
虽然GITHUB等网站可以根据语言种类预设.gitignore文件,但是当项目中涉及多种语言,或者项目中存在一些测试过程中生产的临时文件,需要手动编辑一下。
注意:.gitignore只能作用于未被加入版本控制的文件,对于已经加入版本控制的文件,需要先git rm file,在添加到.gitignore中。
.gitignore有两个模式,开放模式和保守模式。
顾名思义,开放模式表示符合.gitignore中的配置的文件会被忽略。例如:
#
# NOTE! Don't add files that are generated in specific
# subdirectories here. Add them in the ".gitignore" file
# in that subdirectory instead.
#
# NOTE! Please use 'git ls-files -i --exclude-standard'
# command after changing this file, to see if there are
# any tracked files which get ignored after the change.
#
# Normal rules
#
.*
*.o
*.o.*
*.a
*.s
*.ko
*.so
*.so.dbg
*.mod.c
*.i
*.lst
*.symtypes
*.order
*.elf
*.bin
*.gz
*.bz2
*.lzma
*.xz
*.lz4
*.lzo
*.patch
*.gcno
modules.builtin
Module.symvers
而保守模式则是表示确保那些文件需要被跟踪。例如:
#
# git files that we don't want to ignore even it they are dot-files
#
!.gitignore
!.mailmap
这样可以确保.gitignore和.mailmap两个文件被排除在外。
一般而言,对于只想追踪某个文件夹下几个特定的文件,而其他全部忽略,可以使用保守模式和开放模式并用的方法:
/DIR
!/DIR/ImportFile.c
附录:.gitignore的一个例子
# 此为注释 – 将被 Git 忽略
# 忽略所有 .a 结尾的文件
*.a # 但 lib.a 除外
!lib.a # 仅仅忽略项目根目录下的 TODO 文件,不包括 subdir/TODO
/TODO # 忽略 build/ 目录下的所有文件
build/
# 会忽略 doc/notes.txt 但不包括 doc/server/arch.txt
doc/*.txt
.gitignore使用glob模式匹配。目前.gitignore不支持正则表达式。至少我的目前是不支持的。