详解Git.gitignore开发必备建议收藏
作者:Linux猿 时间:2022-04-29 13:44:13
目录
一、为什么使用 .gitignore ?
二、使用规则
2.1 注释
2.2 忽略文件
(1)忽略文件和目录
(2)仅忽略文件
2.3 忽略目录
2.4 使用通配符
2.5 反向操作
2.6 双星号
2.7 其它规则
三、总结
在使用 git 管理项目过程中,.gitignore 文件是必备的文件,下面来详细说一说!
一、为什么使用 .gitignore ?
在一些项目中,我们不想让本地仓库的所有文件都上传到远程仓库中,而是有选择的上传,比如:一些依赖文件(node_modules下的依赖)、bin 目录下的文件、测试文件等。一方面将一些依赖、测试文件都上传到远程传输量很大,另一方面,一些文件对于你这边是可用的,在另一个人那可能就不可用了,比如:本地配置文件。
为了解决上述问题,git 引入了 .gitignore 文件,使用该文件来选择性的上传文件。
二、使用规则
2.1 注释
注释使用 # 开头,后面跟注释内容。如下所示:
linuxy@linuxy:~/linuxGit$ cat .gitignore
# this is .gitignore file.
# 以下是忽略的文件
out
*.exe
linuxy@linuxy:~/linuxGit$
上例中,以 # 开头的便是注释。
2.2 忽略文件
(1)忽略文件和目录
例如:folderName : 表示忽略 folderName 文件和 folderName 目录,会自动搜索多级目录,比如:*/*/folderName。
来看一个简单的例子,本地仓库的目录结构如下所示:
linuxy@linuxy:~/linuxGit$ tree
.
├── folder
│ └── file1
└── src
├── folder
└── utils
└── folder
3 directories, 3 files
linuxy@linuxy:~/linuxGit$
其中,.gitignore 文件内容如下所示:
linuxy@linuxy:~/linuxGit$ cat .gitignore
# this is .gitignore file.
# 以下是忽略的文件
folder
linuxy@linuxy:~/linuxGit$
故在本地仓库中,同名的 folder 目录、src/folder 文件、src/utils/folder 文件都会被忽略,即:不会被提交到远程仓库中。
(2)仅忽略文件
模式如下所示:
folderName
!folderName/
仅忽略 folderName 文件,而不忽略 folderName 目录,其中,感叹号“!”表示反向操作。
来看一个简单的例子,本地仓库的目录结构如下所示:
linuxy@linuxy:~/linuxGit$ tree
.
├── folder
│ └── file1
└── src
├── folder
└── utils
└── folder
3 directories, 3 files
linuxy@linuxy:~/linuxGit$
其中,.gitignore 文件内容如下所示:
linuxy@linuxy:~/linuxGit$ cat .gitignore
# this is .gitignore file.
# 以下是忽略的文件
folder
!folder/
linuxy@linuxy:~/linuxGit$
故在本地仓库中,src/folder 文件、src/utils/folder 文件会被忽略,而同名的 folder 目录不会被忽略。
2.3 忽略目录
模式如下所示:
folderName/
忽略 folderName 目录,包括:
(1)当前目录下的foldernName,例如:folderName/;
(2)多级目录下的 folderName,例如:*/*/folderName/;
来看一个简单的例子,本地仓库的目录结构如下所示:
linuxy@linuxy:~/linuxGit$ tree
.
├── folder
│ └── file1
└── src
├── folder
└── utils
└── folder
3 directories, 3 files
linuxy@linuxy:~/linuxGit$
其中,.gitignore 文件内容如下所示:
linuxy@linuxy:~/linuxGit$ cat .gitignore
# this is .gitignore file.
# 以下是忽略的文件
folder/
linuxy@linuxy:~/linuxGit$
故在本地仓库中,folder 目录会被忽略,而同名的 src/folder 文件和 src/utils/folder 文件不会被忽略。
2.4 使用通配符
常用的通配符有:
(1)星号“*” :匹配多个字符;
(2)问号“?”:匹配除 ‘/'外的任意一个字符;
(3)方括号“[xxxx]”:匹配多个列表中的字符;
来看一个简单的例子,本地仓库的目录结构如下所示:
linuxy@linuxy:~/linuxGit$ tree
.
├── src
│ ├── add.c
│ ├── add.i
│ └── add.o
├── test.c
├── test.i
└── test.o
1 directory, 6 files
linuxy@linuxy:~/linuxGit$
其中,.gitignore 文件内容如下所示:
linuxy@linuxy:~/linuxGit$ cat .gitignore
# this is .gitignore file.
# 以下是忽略的文件
*.[io]
linuxy@linuxy:~/linuxGit$
故在本地仓库中,test.i文件、test.o文件、src/add.o文件、src/add.i文件会被忽略,而 test.c文件和add.c 文件不会被忽略。注意:这里忽略的匹配模式是多级目录的。
2.5 反向操作
模式如下所示:
!匹配模式
表示之前忽略的匹配模式再次包含在跟踪内容里。
例如在仅忽略文件时提到的模式:
folderName
!folderName/
表示仅忽略 folderName 文件,而不忽略 folderName 目录。
2.6 双星号
斜杠后紧跟两个连续的星号"**",表示多级目录。
来看一个简单的例子,.gitignore文件的内容如下所示:
linuxy@linuxy:~/linuxGit$ cat .gitignore
# this is .gitignore file.
# 以下是忽略的文件
src/**/file
linuxy@linuxy:~/linuxGit$
2.7 其它规则
(1)空行不匹配任何文件;
(2)git 跟踪文件,而不是目录;
(3)在 .gitignore 文件中,每行表示一种模式;
(4)如果本地仓库文件已被跟踪,那么即使在 .gitignore 中设置了忽略,也不起作用。
(5).gitignore 文件也会被上传的到远程仓库,所以,同一个仓库的人可以使用同一个.gitignore 文件。
三、总结
在使用 git 过程中,掌握 .gitignore 的使用很重要,可以减少不必要的文件上传到远程。
来源:https://blog.csdn.net/nyist_zxp/article/details/119887324
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python编程matplotlib交互绘制Julia集示例解析
![](https://img.aspxhome.com/file/2023/0/66370_0s.jpg)
python爬取梨视频生活板块最热视频
![](https://img.aspxhome.com/file/2023/7/135257_0s.jpg)
Django在win10下的安装并创建工程
![](https://img.aspxhome.com/file/2023/6/100846_0s.png)
Python fire模块(最简化命令行生成工具)的使用教程详解
![](https://img.aspxhome.com/file/2023/0/105280_0s.png)
SQLServer 存储过程简介与使用方法
python使用pynput库操作、监控你的鼠标和键盘
![](https://img.aspxhome.com/file/2023/3/124793_0s.png)
python中的np.argmax() 返回最大值索引号
![](https://img.aspxhome.com/file/2023/0/67860_0s.png)
python Django框架实现自定义表单提交
Python模拟登录和登录跳转的参考示例
Mac 安装 Python3.10 和 配置环境的详细教程
![](https://img.aspxhome.com/file/2023/2/115122_0s.jpg)
网页制作 JSP与ASP 的比较
Python全局变量global关键字详解
Oracle merge合并更新函数实例详解
miniconda3介绍、安装以及使用教程
![](https://img.aspxhome.com/file/2023/6/127296_0s.jpg)
python基础while循环及if判断的实例讲解
Python数值求解微分方程方法(欧拉法,隐式欧拉)
![](https://img.aspxhome.com/file/2023/2/87782_0s.png)
对python中list的拷贝与numpy的array的拷贝详解
![](https://img.aspxhome.com/file/2023/2/68952_0s.jpg)
python中threading和queue库实现多线程编程
C#调用Python的URL接口的示例
![](https://img.aspxhome.com/file/2023/4/71744_0s.png)