git 忽略文件 .gitignore 以及规则
git提供了文件忽略系统,当对工作区某个目录或文件设置了忽略后,在执行status查看状态时,被忽略的文件即使存在也不会显示出来。
这样我就可以把那些不需要上传,不需要保留的文件或目录忽略掉(比如一些动态生成的log,或者编译出来的文件等等)。
对于忽略文件,git提供了3种方式(我们组的大神又告诉我了另一种方法)下面来一一介绍一下:
一:.gitignore设置远程共享忽略文件
忽略文件.gitignore使用:
首先可以从文件名看出.gitignore是一个隐藏文件 一般我们默认会把它建立在仓库的根目录(也可以是仓库下的任意目录)
如下:
cd
到根目录;
ls -a
查看所有隐藏文件;
vim .gitignore
创建或编辑.gitignore文件,将需要忽略的文件写在其中;
忽略文件的规则:
*.a 忽略所有以.a为后缀的文件;
!lib.a 不忽略文件lib.a;
/TODO 只忽略此目录下TODO文件,子目录的TODO不被忽略;
build/ 忽略build目录下的所有文件;
doc/*.txt 只忽略doc/下所有的txt文件,但是不忽略doc/subdir/下的txt文件;
小技巧:
- 文件.gitignore可以放在当前仓库中的任何目录中;
- 忽略只对未跟踪文件有效,对于已经加入版本库的文件无效;
- 如果不希望将.gitignore添加到库里,也不希望.gitignore文件带来任何干扰,可以在忽略文件中忽略自己;
二:本地独享
上面设置的.gitignore通常都会上传到远端的版本库中,所以它是“共享式”的,而在有的情况下我们需要有一个自己“独享式”的本地忽略文件。
具体版本库:
在该版本库.git/info/exclude
来设置文件忽略,或者也可以使用.gitignore文件来忽略自己可以达到同样效果
全局版本库:
全局忽略是指忽略本地中所有的版本库。
通过设置git config:
$ git config --global core.excludefile /dean/.gitignore_global
/dean/.gitignore_global 是一个自定义的忽略文件,这个文件的目录、名字和里面的内容都按不同需求任意设定。
大神告诉的一个方法:
进入到本机的.config/git中,里面有一个ignore文件。
我们在这个文件中加上想要忽略的就可以起到全局忽略的效果。
可以使用如下命令很方便: vim .config/git/ignore
忽略文件规则
git 对于 .gitignore 配置文件是按行从上到下进行规则匹配的,如果前面的规则匹配的范围更大,则后面的规则将不会生效;
忽略规则是对文件名有效的
A: 空行或#号开始的行,会被忽略;
B: 可以使用通配符:
* 任意字符;
? 单个字符;
[abc] 多种可能的字符a、b或c;
[a-z0-9] 表示在某个范围内进行匹配;
\ 转义字符;
! 表示取反(不忽略),写在某条规则的前面;
C: 路径分隔符"/";
如果"/"后面的名称是个目录,则该目录以及该目录下的所有文件都会被忽略;
如果"/"后面的名称是个文件,则该文件不会被忽略;
例如: /name
如果name是个目录,则目录name和name下的所有文件都会被忽略;
如果name是个文件,则该文件不会被忽略;
D: .gitignore文件也可以忽略自己,只要把自己的名字写进来即可;
E: 一条(行)忽略规则只对某一个目录下的文件有效,而对该目录下的子目录中的文件无效;
F: 一条(行)忽略规则也可以只对单个文件有效(忽略单个指定的文件);
例如:
*.a #忽略所有以.a为后缀的文件;
!lib.a #不忽略文件lib.a;
/TODO #只忽略此目录下TODO文件,子目录的TODO不被忽略;
build/ #忽略当前目录之下的 build 目录下的所有文件;
build #忽略 任何目录 build文件;
/build #忽略build根目录下的build文件;
/build/ #忽略build根目录下的build目录下的所有文件;
doc/*.txt #只忽略doc/下所有的txt文件,但是不忽略doc/subdir/下的txt文件;
可以使用标准的 glob 模式匹配
所谓的 glob 模式是指 shell 所使用的简化了的正则表达式。
星号(*
)匹配零个或多个任意字符;
[abc]
匹配任何一个列在方括号中的字符(这个例子要么匹配一个 a,要么匹配一个 b,要么匹配一个 c);
问号(?
)只匹配一个任意字符;
如果在方括号中使用短划线分隔两个字符,表示所有在这两个字符范围内的都可以匹配(比如 [0-9]
表示匹配所有 0 到 9 的数字)。
使用两个星号(*
) 表示匹配任意中间目录,比如a/**/z
可以匹配 a/z
, a/b/z
或 a/b/c/z
等。
• 可以使用标准的 glob 模式匹配。
• 匹配模式最后跟反斜杠(/)说明要忽略的是目录。
• 要忽略指定模式以外的文件或目录,可以在模式前加上惊叹号(!)取反。
# 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
规则:.DBStore/*
说明:忽略.gitignore文件所在的目录之下的任何位置的目录 .DBStore 下的全部内容;
注意,不管是根目录下的 /.DBStore/ 目录,
还是某个子目录 /lib/.DBStore/ 目录,都会被忽略;
规则:/upload/*
说明:忽略根目录下的 /upload/ 目录的全部内容;
规则:!.gitignore
跟踪 .gitignore 文件
说明:忽略全部内容<如果存在一行 * >,但是不忽略 .gitignore 文件
如果要忽略的文件已被git管理,需要先移除,命令如下: -r:递归
If you already have a file checked in, and you want to ignore it,
Git will not ignore the file if you add a rule later.
In those cases, you must untrack the file first, by running the following command in your terminal:
git rm -r --cached WebRoot/WEB-INF/classes/**/*
PATTERN FORMAT
A blank line matches no files, so it can serve as a separator for readability.
A line starting with # serves as a comment. Put a backslash ("
\
") in front of the first hash for patterns that begin with a hash.Trailing spaces are ignored unless they are quoted with backslash ("
\
").An optional prefix "
!
" which negates the pattern;
any matching file excluded by a previous pattern will become included again.
It is not possible to re-include a file if a parent directory of that file is excluded.
Git doesn’t list excluded directories for performance reasons,
so any patterns on contained files have no effect, no matter where they are defined.
Put a backslash ("\
") in front of the first "!
" for patterns that begin with a literal "!
", for example,
"\!important.txt
".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 directoryfoo
and paths underneath it,
but will not match a regular file or a symbolic linkfoo
(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).Otherwise, Git treats the pattern as a shell glob suitable for consumption by fnmatch(3) with the FNM_PATHNAME flag:
wildcards in the pattern will not match a / in the pathname.
For example, "Documentation/*.html" matches "Documentation/git.html"
but not "Documentation/ppc/ppc.html" or "tools/perf/Documentation/perf.html".A leading slash matches the beginning of the pathname.
For example, "/*.c" matches "cat-file.c" but not "mozilla-sha1/sha1.c".
Two consecutive asterisks ("**
") in patterns matched against full pathname may have special meaning:
A leading "
**
" followed by a slash means match in all directories.
For example,
"**/foo
" matches file or directory "foo
" anywhere, the same as pattern "foo
".
"**/foo/bar
" matches file or directory "bar
" anywhere that is directly under directory "foo
".A trailing "
/**
" matches everything inside.
For example, "abc/**
" matches all files inside directory "abc
",
relative to the location of the.gitignore
file, with infinite depth.
"abc/*.*" : relative to the location of the.gitignore
file onlyA slash / followed by two consecutive asterisks then a slash matches zero or more directories.
For example, "a/**/b
" matches "a/b
", "a/x/b
", "a/x/y/b
" and so on.Other consecutive asterisks are considered invalid.
git 忽略文件 .gitignore 以及规则的更多相关文章
- Git忽略文件.gitignore的使用
本博客旨在自我学习使用,如有任何疑问请及时联系博主 1.WHY? 当你使用git add .的时候有没有遇到把你不想提交的文件也添加到了缓存中去?比如项目的本地配置信息,如果你上传到Git中去其他人p ...
- git 忽略文件.gitignore
# 此为注释– 将被Git 忽略 *.a # 忽略所有.a 结尾的文件 !lib.a # 但lib.a 除外 /TODO # 仅仅忽略项目根目录下的TODO 文件,不包括subdir/TODO bui ...
- git 忽略文件[.gitignore]常用配置
.idea .buildpath .project .settings .Ds_Store composer.json composer.lock a.php /public/uploads /run ...
- Git忽略文件方法【转】
转自:http://www.cnblogs.com/shangdawei/archive/2012/09/08/2676669.html http://cwind.iteye.com/blog/166 ...
- git忽略文件【转】
转自: http://cwind.iteye.com/blog/1666646 有很多文件不必使用git管理.例如Eclipse或其他IDE生成的项目文件,编译生成的各种目标或临时文件等.使用git ...
- Git 忽略提交 .gitignore
在使用Git的过程中,我们喜欢有的文件比如日志,临时文件,编译的中间文件等不要提交到代码仓库,这时就要设置相应的忽略规则,来忽略这些文件的提交. Git 忽略文件提交的方法 有三种方法可以实现忽略Gi ...
- git忽略文件不起作用时
开始我是直接进到仓库建立了.gitignore文件,再从仓库进入到项目add时总是会添加不需要添加的文件, 后来明白应该是在哪里提交在哪里创建.gitignore文件 git忽略文件操作步骤如下: 1 ...
- 详解Github的.gitignore忽略文件+.gitignore不生效解决方案+生产配置大奉送
详解Github的.gitignore忽略文件+.gitignore不生效解决方案+生产配置大奉送 Git中有一个非常重要的一个文件-----.gitignore 今天给大家免费送一个.gitigno ...
- 添加git 忽略文件
在使用Git的过程中,我们喜欢有的文件比如日志,临时文件,编译的中间文件等不要提交到代码仓库,这时就要设置相应的忽略规则,来忽略这些文件的提交. Git 忽略文件提交的方法 有三种方法可以实现忽略Gi ...
随机推荐
- Luogu P1750 【出栈序列】
一眼(万年)贪心minn设小调不出来祭 首先要保证更靠前的输出更小那么容易想到,对于之后可能入栈的元素(即栈的剩余空间仍能装下的所有元素),我们可以取其中的最小值minn,和栈顶元素$top$比较,如 ...
- 让你的 JMeter 像 LoadRunner 那样实时查看每秒事务数(TPS)、事务响应时间(TRT)
熟悉 LoadRunner 的朋友一定不会对其 TPS(每秒事务数).TRT(事务响应时间) 等视图感到陌生,因为这是压力测试最为关键的两个指标.JMeter 以其开源.轻巧.灵活.扩展性高等特性赢得 ...
- Linux学习笔记:vi常用命令
在Linux系统中常用vi命令进行文本编辑. vi命令是UNIX操作系统和类UNIX操作系统中最通用的全屏幕纯文本编辑器.Linux中的vi编辑器叫vim,它是vi的增强版(vi Improved), ...
- JS正则表达式方法
使用正则表达式的主要有match,exec,test 1.正则表达式方法test测试给定的字符串是否满足正则表达式,返回值是bool类型的,只有真和假. var user_code = $(" ...
- 在Eclipse中导入新浪微博SDK
在Eclipse中导入新浪微博SDK 今天在看<Android开发应用实战>,全书都在讲一个android版的新浪微博客户端怎么做,于是按照书上步骤做.网上有人说这本书没有细节,我想对于小 ...
- git clone 某个分支或者所有分支
clone 某个分支: git clone -b dev5 https://git.coding.net/aiyongbao/tradepc.git clone 所有分支: git cl ...
- Winfom 插件式(Plugins)/模块化开发框架-动态加载DLL窗体-Devexpress
插件式(AddIn)架构,不是一个新名词,应用程序采用插件式拼合,可以更好的支持扩展.很多著名的软件都采用了插件式的架构,如常见的IDE:Eclipse,Visual Studio,SharpDeve ...
- android拾遗——Android之Notification和NotificationManager
1.使用系统自带的Notification //创建一个NotificationManager的引用 String ns = Context.NOTIFICATION_SERVICE; Notific ...
- Android Activity、Service、BroadcastReceiver 的生命周期
Activity.Service.BroadcastReceiver这三个组建是Android开发中最常使用到的组件,在它们的生命周期的各个阶段我们需要针对性的做些事情,了解这些组件的生命周期有利于我 ...
- 站点的安全防范都是后端的职责?非也,Web前端安全同样不可忽视
前言 随着网络的快速普及,网络安全问题的受害者不再只是政府.企业等集体,每一个接触网络的普通人都有可能成为网络攻击的受害者.随着网络的普及,黑客进行网络攻击的手段越来也多,越来越复杂.以网站的攻击为例 ...