Git是怎么Ignore文件的?

Git is one of the most popular version control systems (VCS) available, especially thanks to hosting vendors like GitHub. It keeps code safe and shareable. Sometimes, however, certain files should not be shared, like local settings or temporary configs. Git provides a few ways to make sure those files are ignored.
.gitignore
The easiest and most common way to ignore files is to use a gitignore file. Simply create a file named .gitignore in the repository’s root directory. Then, add names and patterns for any files and directories that should not be added to the repository. Use the asterisk (“*”) as a wildcard. For example, “*.class” will ignore all files that have the “.class” extension. Remember to add the .gitignore file to the repository so that it can be shared. As a bonus, Git hosting vendors like GitHub usually provide standard .gitignore templates for popular languages.
Any files covered by the .gitignore file will not be added to the repository. This approach is ideal for local IDE settings like .idea or .vscode, compiler output files like *.class or *.pyc, and test reports.
.git/info/exclude
As a best practice, .gitignore should be committed to the repository, which means all team members will share the same set of ignored files. However, some files should be ignored locally and not globally. Those files could be added to .gitignore, but large .gitignore files become cryptic and more likely to break other people’s setup. Thankfully, git provides a local-only solution: the .git/info/exclude file (under the repository’s hidden .git directory). Simply open it with a text editor and add new entries using the same file pattern format as .gitignore.
# Append a new file to ignore locally
echo "my_private_file" >> .git/info/exclude
skip-worktree
A .gitignore file prevents a file from being added to a repository, but what about preventing changes from being committed to an existing file? For example, developers may want to safely override settings in a shared config file for local testing. That’s where skip-worktree comes in: it allows a developer to “skip” any local changes made to a given file. Changes will not appear under “git status” and thus will not be committed.
Use the following commands:
# Ignore local changes to an existing file
git update-index --skip-worktree path/to/file # Stop ignoring local changes
git update-index --no-skip-worktree path/to/file
Warning: The skip-worktree setting applies only to the local repository. It is not applied globally! Each developer will need to run the skip-worktree command in their local repository.
assume-unchanged
Another option for ignoring files is assume-unchanged. Like skip-worktree, it makes Git ignore changes to files. However, whereas skip-worktree assumes that the user intends to change the file, assume-unchanged assumes that the user will not change the file. The intention is different. Large projects using slow file systems may gain significant performance optimizations by marking unused directories as assume-unchanged. This option also works with other update-index options like really-refresh.
Use the following commands:
# Assume a file will be unchanged
git update-index --assume-unchanged path/to/file # Undo that assumption
git update-index --no-assume-unchanged path/to/file
Again, this setting applies only to the local repository – it is not applied globally.
Comparison Table
Which is the best way to ignore files?
| METHOD | DESCRIPTION | BEST USE CASES | SCOPE |
| .gitignore file | Prevents files from being added to the repository. | Local settings, compiler output, test results, etc. | Global |
| .git/info/exclude file | Prevents local files from being added to the repository. | Local settings, compiler output, test results, etc. | Local |
| skip-worktree setting | Prevents local changes from being committed to an existing file. | Shared files that will have local overwrites, like config files. | Local |
| assume-unchanged setting | Allows Git to skip files that won’t be changed for performance optimization. | Files and folders that a developer won’t touch. | Local |
引用地址:https://automationpanda.com/2018/09/19/ignoring-files-with-git/
Git是怎么Ignore文件的?的更多相关文章
- 解决Git在添加ignore文件之前就提交了项目无法再过滤问题
由于未添加ignore文件造成提交的项目很大(包含生成的二进制文件).所以我们可以将编译生成的文件进行过滤,避免添加到版本库中了. 首先为避免冲突需要先同步下远程仓库 $ git pull 在本地项目 ...
- 解决git .ignore文件无效
在用 Git 进行代码管理的时候,我们会用 .gitignore 文件来描述哪些文件是不需要进行版本管理的,也就是被忽略掉. 如果我们在第一次提交的时候,忘记添加 .gitignore 文件或者在首次 ...
- [git] ignore文件规则失效
背景 在某次项目,发现已经将.iml规则写进.ignore文件,但是对.iml的修改依然会出现在changelist中. 解决方案 先引用git官网上的描述 gitignore - Specifies ...
- git ignore文件
创建.gitignore文件来设置git要忽略的文件模式: 官方文件列列表:https://github.com/github/gitignore 1. 所有空行或者以 # 开头的行都会被 Git 忽 ...
- intellj Idea git ignore文件的.idea不起作用解决
问题描述: idea中使用git每次提交的时候都会选中项目目录下.idea目录,虽然设置了.ignore文件但是不起作用. 综合网上搜索结果,并完美解决,方法如下: 1.原因就是git已经关联追踪了这 ...
- Git ignore文件的用法
这周为了往自己个人代码仓库里囤货,把在公司写的一些东西上传到了自己的GitHub代码仓库,手抖把测试用的日志也一并上传了.上传没多长时间就被运维找上门了,说commit里包含内网相关信息,要求删除.当 ...
- git常见操作--忽略文件以及常用命令【转】
转自:http://www.cnblogs.com/elfsundae/archive/2011/07/17/2099698.html References: http://stackoverflow ...
- Git版本控制:Git查阅、撤销文件修改和撤销文件追踪
http://blog.csdn.net/pipisorry/article/details/47867097 查看文件的修改历史 git log --pretty=oneline 文件名 # 显示修 ...
- git 忽略 IntelliJ .idea文件
git 忽略 IntelliJ .idea文件 2016年10月22日 11:31:27 阅读数:6196 标签: git 更多 个人分类: git 版权声明:本文为博主原创文章,未经博主允许不得 ...
随机推荐
- 虹软人脸识别SDK在网络摄像头中的实际应用
目前在人脸识别领域中,网络摄像头的使用很普遍,但接入网络摄像头和人脸识别SDK有一定门槛,在此篇中介绍过虹软人脸识别SDK的接入流程,本文着重介绍网络摄像头获取视频流并处理的流程(红色框内),以下内容 ...
- Java8新特性概览
Java8新特性简介 a)速度更快 1.对于JVM内存模型的新定义,将永久代从堆内存中移除,以前HotSpot JVM堆内存分为三块:1.年轻代 2.年老代 3.持久代 点击回顾 取而代之的是 ...
- (五)react-native开发系列之Android原生交互
react-native可以做web与原生的交互,这是使用react-native开发项目的主要目的之一,也是主要优势,用rn而不用原生交互则毫无价值,这篇文章用来记录在项目中rn的原生交互使用过程. ...
- ssh远程登录连接慢的解决方法
近期在搭建自动化集群服务,写脚本ssh批量分发公钥至其它服务器时比较缓慢,便在度娘上寻找解决方法如下: 方法一: 以ssh -v 调试模式远程登录: [root@bqh-nfs- ceshi]# ss ...
- AxureRP分页签 / Tab选项卡切换功能~
最终结果图如下: 实现过程: 1.从元件库中拖一个动态面板,调整所需大小,接下来的步骤都通过双击动态面板来完成. 2.双击动态面板,弹出框“面板状态管理”,新建状态并命名.此处新建了TAB1.TAB2 ...
- 工作中 99% 能用到的 Git 命令
分支操作 暂存操作 回退操作 标签操作 常规操作 git创建项目仓库 忽略已加入到版本库中的文件 取消忽略文件 拉取.上传免密码. 分支操作 git branch 创建分支 git branch -b ...
- 结构型模式(六) 享元模式(Flyweight)
一.动机(Motivate) 在软件系统中,采用纯粹对象方案的问题在于大量细粒度的对象会很快充斥在系统中,从而带来很高的运行时代价--主要指内存需求方面的代价.如何在避免大量细粒度对象问题的同时,让外 ...
- 项目前端 - vue配置 | axios配置 | cookies配置 | element-ui配置 | bootstrap配置
vue项目创建 环境 1.傻瓜式安装node: 官网下载:https://nodejs.org/zh-cn/ 2.安装cnpm: >: npm install -g cnpm --regis ...
- win10 安装python模块objgraph+PyCharm环境配置
1. 打开win10的命令行窗口 2.在命令行中输入python -m pip install objgraph,系统会自动帮忙安装 3.安装完成后,可以用命令python -m pip list查看 ...
- go函数类型的使用
Go函数类型的使用 type myfunc func() string // 声明函数变量 func main() { fn := func() string { return "bbb&q ...