Git .ignore文件忽略注意事项

时间:2022-12-18 17:15:56

虽然一直使用git,忽略文件经常出问题,所以整理一下.

1. 本地仓库忽略本地仓库的文件忽略规则可以在.git/info/exclude文件中添加。这些忽略的文件不会提交到共享库中,因而不会被协作者所共享。
2. 当前工作目录添加文件忽略对于每一级工作目录,创建一个.gitignore文件,向该文件中添加要忽略的文件或目录。但在创建并编辑这个文件之前,一定要保证要忽略的文件没有添加到git索引中。使用命令git rm --cached filename将要忽略的文件从索引中删除。--摘抄.gitignore的格式规范• 所有空行或者以注释符号 # 开头的行都会被 Git 忽略。
• 可以使用标准的 glob 模式匹配。
• 匹配模式最后跟反斜杠(/)说明要忽略的是目录。
• 要忽略指定模式以外的文件或目录,可以在模式前加上惊叹号(!)取反。
所谓的 glob 模式是指 shell 所使用的简化了的正则表达式。星号(*)匹配零个或多个任意字符;[abc] 匹配任何一个列在方括号中的字符(这个例子要么匹配一个 a,要么匹配一个 b,要么匹配一个 c);问号(?)只匹配一个任意字符;如果在方括号中使用短划线分隔两个字符,表示所有在这两个字符范围内的都可以匹配(比如[0-9]表示匹配所有 0 到 9 的数字)。
2.1 工作目录的每一层下级目录都可以有一个.gitignore文件,以说明当前目录下需要被git忽略的文件或目录2.2 .gitignore文件可以被提交到共享库中被协作者共享
3. 全局的.gitignore可以通过创建~/.gitignore_global并添加到git全局配置以减少每层目录的规则重复定义。使用命令git config --global core.excludesfile ~/.gitignore_global即可.gitignore_global文件范例
<span style="font-size:12px;"># Compiled source #
###################
*.com
*.class
*.dll
*.exe
*.o
*.so

# Packages #
############
# it's better to unpack these files and commit the raw source
# git has its own built in compression methods
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip

# Logs and databases #
######################
*.log
*.sql
*.sqlite

# OS generated files #
######################
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
Icon?
ehthumbs.db
Thumbs.db</span>
<span style="font-size:12px;"></span><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.2px;"><span style="font-size:12px;"> <strong>References:</strong></span></p><div style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.2px;"><div><span style="font-size:12px;">1. Ignoring files, <a target=_blank href="https://help.github.com/articles/ignoring-files" style="color: rgb(16, 138, 198);">https://help.github.com/articles/ignoring-files</a></span></div><div><span style="font-size:12px;">2. 初次使用git-忽略某些文件, <a target=_blank href="http://www.phperblog.net/?p=173" style="color: rgb(16, 138, 198);">http://www.phperblog.net/?p=173</a></span></div><div><span style="font-size:12px;">3. git ignore file, Git增加忽略文件, <a target=_blank href="http://www.cnblogs.com/wucg/archive/2011/08/16/2141647.html" style="color: rgb(16, 138, 198);">http://www.cnblogs.com/wucg/archive/2011/08/16/2141647.html</a></span></div></div>