略微加速

略速 - 互联网笔记

Git忽略文件配置

2018-01-19 leiting (2809阅读)

标签 Git

1. 本地仓库忽略


本地仓库的文件忽略规则可以在 .git/info/exclude 文件中添加。这些忽略的文件不会提交到共享库中,因而不会被协作者所共享。


# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
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文件范例
复制代码
复制代码
# 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  
复制代码
复制代码
文件 .gitignore 的格式规范如下:


• 所有空行或者以注释符号 # 开头的行都会被 Git 忽略。
• 可以使用标准的 glob 模式匹配。
• 匹配模式最后跟反斜杠(/)说明要忽略的是目录。
• 要忽略指定模式以外的文件或目录,可以在模式前加上惊叹号(!)取反。


所谓的 glob 模式是指 shell 所使用的简化了的正则表达式。
星号(*)匹配零个或多个任意字符;
[abc] 匹配任何一个列在方括号中的字符(这个例子要么匹配一个 a,要么匹配一个 b,要么匹配一个 c);
问号(?)只匹配一个任意字符;
如果在方括号中使用短划线分隔两个字符,表示所有在这两个字符范围内的都可以匹配(比如[0-9]表示匹配所有 0 到 9 的数字)。


我们再看一个 .gitignore 文件的例子:


# 此为注释 – 将被 Git 忽略
*.a # 忽略所有 .a 结尾的文件
!lib.a # 但 lib.a 除外
/TODO # 仅仅忽略项目根目录下的 TODO 文件,不包括 subdir/TODO
build/ # 忽略 build/ 目录下的所有文件
doc/*.txt # 会忽略 doc/notes.txt 但不包括 doc/server/arch.txt
复制代码
Two things to keep in mind with ignoring files:


First, if a file is already being tracked by Git, adding the file to .gitignore won’t stop Git from tracking it.


You’ll need to do git rm --cached <file> to keep the file in your tree and then ignore it.


Secondly, empty directories do not get tracked by Git.


If you want them to be tracked, they need to have something in them. 
Usually doing a touch .gitignore is enough to keep the folder tracked.


You can also open up $GIT_DIR/info/exclude ($GIT_DIR is usually your .git folder) and edit that file for project-only ignores. 
The problem with this is that those changes aren’t checked in, 
so use this only if you have some personal files that don’t need to be shared with others on the same project.


Your final option with ignoring folders is adding a per-user ignore by setting up acore.excludesfiles option in your config file. 
You can set up a .gitignore file in yourHOME directory that will affect all of your repositories by running this command:


git config --global core.excludesfile ~/.gitignore


Read up on the manpage if you’d like to learn more about how ignores work. 
As always, if you have other ignore-related tips let us know in the comments.


 


git忽略文件有三种:




1、全局范围内有效的忽略文件


就是"版本库根目录/.git/info/exclude",全局范围内的所有忽略规则都以行为单位写在这个文件中;




2、局部范围内有效的忽略文件


就是.gitignore,这个忽略文件只对某一级目录下的文件的忽略有效;
如果某一个目录下有需要被忽略的文件,那么就可以在该目录下手工地创建忽略文件.gitignore,
并在这个忽略文件中写上忽略规则,以行为单位,一条规则占据一行;
比较特殊的情况就是在版本库的根目录下创建一个忽略文件.gitignore,这时,
这个.gitignore忽略文件就对版本库根目录下的文件有效,等价于全局范围内的忽略文件.git/info/exclude;




3、手工指定一个忽略文件,


该忽略文件中的规则和语法与前两种是一致的,随便哪一级目录都可以,只要加上对应的路径即可;
   手工指定忽略文件的命令是:
   git config --global core.excludesfile /path/to/.gitignore
   然后手工地在对应位置创建忽略文件.gitignore,并在该文件中写入忽略规则即可;




备注:
这三种范围级别的忽略文件的内容格式和语法都是一致的;


 


.gitignor文件同样可以像其它文件一样加到项目仓库里( 直接用 git add .gitignore 和 git commit等命令), 


这样项目里的其它开发者也能共享同一套忽略 文件规则。


 


如果想要连.gitignore文件自己也忽略呢?一样的道理,只要在文件内加上.gitignore即可。


 


如果你想忽略规则只对特定的仓库起作用,你可以把这些忽略规则写到你的仓库下


 


.git/info/exclude 文件中,或是写在Git配置变量


 


core.excludesfile 中指定的 文件里。


 


有些Git命令也可在命令行参数中指定忽略规则,你可以在这里:gitignore 查看详细的用法。


 


复制代码
github中不加入版本控制.gitignore设定
JAN 19TH, 2012
这几天研究github page,由于用jekyll本地测试时会生成_site文件夹,
所以需要把这个文件夹排除在外再提交到github,到底怎么设置呢?


这就需要用到.gitignore,即不加入版本控制,在git根目录下建立.gitignore,具体设定如下:


tmp.txt         //忽略tmp.txt
*.log           //忽略所有log文件
tmp/*           //忽略tmp文件夹所有文件
log/**/*.log    //忽略log目录下的包括子目录下的所有log文件
其他的一些过滤条件


?:代表任意的一个字符
*:代表任意数目的字符
{!ab}:必须不是此类型
{ab,bb,cx}:代表ab,bb,cx中任一类型即可
[abc]:代表a,b,c中任一字符即可
[ ^abc]:代表必须不是a,b,c中任一字符
由于git不会加入空目录,所以下面做法会导致tmp不会存在


tmp/*             //忽略tmp文件夹所有文件
改下方法,在tmp下也加一个.gitignore,内容为


*
!.gitignore
还有一种情况,就是已经commit了,再加入gitignore是无效的,所以需要删除下缓存


git rm --cached ignore_file
这样就OK了。

复制代码


文章转自 http://www.cnblogs.com/shangdawei/archive/2012/09/08/2676669.html


北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3