Git 忽略规则 .gitignore文件 MD
| Markdown版本笔记 | 我的GitHub首页 | 我的博客 | 我的微信 | 我的邮箱 |
|---|---|---|---|---|
| MyAndroidBlogs | baiqiantao | baiqiantao | bqt20094 | baiqiantao@sina.com |
Git 忽略规则 .gitignore文件 MD
目录
添加忽略规则的三种方式
局部 Create a local .gitignore
全局 Create a global .gitignore
个人 Explicit repository excludes
.gitignore 文件时的格式
Android 适用的 .gitignore
补充:让 Git 自动替换文件中的指定内容
添加忽略规则的三种方式
From time to time, there are files you don't want Git to check in to GitHub. There are a few ways to tell Git which files to ignore.
有时候,有一些文件你不希望Git检入GitHub。有几种方法可以告诉Git忽略哪些文件。
局部 Create a local .gitignore
If you create a file in your repository named .gitignore, Git uses it to determine which files and directories to ignore, before you make a commit.
如果您在存储库中创建一个名为.gitignore的文件,Git会在您进行提交之前使用它来确定忽略哪些文件和目录。
A .gitignore file should be committed into your repository, in order to share the ignore rules with any other users that clone the repository.
一个.gitignore文件应该被提交到你的仓库中,以便与任何其他克隆仓库的用户共享这个忽略规则。
GitHub maintains an official list of recommended .gitignore files for many popular operating systems, environments, and languages in the github/gitignore public repository.
GitHub在 .gitignore 公共仓库中维护了一个官方的列表,(列表里面包含了在使用)许多流行的操作系统、环境和语言时的推荐的.gitignore文件。
In Terminal, navigate to the location of your Git repository.
Entertouch .gitignoreto create a .gitignore file.
The Octocat has a Gist containing some good rules to add to this file.
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也将不会忽略这个文件。在这种情况下,您必须先在终端中运行以下命令,以解除文件:
git rm --cached FILENAME # 停止追踪指定文件,但该文件会保留在工作区
全局 Create a global .gitignore
You can also create a global .gitignore file, which is a list of rules for ignoring files in every Git repository on your computer. For example, you might create the file at ~/.gitignore_global and add some rules to it.
您也可以创建一个全局的.gitignore文件,这是一个忽略计算机上每个Git仓库中文件的规则列表。例如,您可以在~/.gitignore_global中创建一个文件,并为其添加一些规则。
Open Terminal.
Run the following command in your terminal:
git config --global core.excludesfile ~/.gitignore_global
The Octocat has a Gist containing some good rules to add to this file.
个人 Explicit repository excludes
explicit [ɪkˈsplɪsɪt] adj. 明确的,清楚的; 直言的; 详述的; 不隐瞒的;
exclude [ɪk'sklu:d] vt. 排斥;排除,不包括;驱除,赶出
If you don't want to create a .gitignore file to share with others, you can create rules that are not committed with the repository. You can use this technique for locally-generated files that you don't expect other users to generate, such as files created by your editor.
如果您不想创建一个与其他人共享的.gitignore文件,那么你可以创建一些不与仓库一起提交的规则。您可以将此技术用于不希望其他用户生成的本地生成的文件,例如编辑器创建的文件。
Use your favorite text editor to open the file called .git/info/exclude within the root of your Git repository. Any rule you add here will not be checked in, and will only ignore files for your local repository.
使用你最喜欢的文本编辑器打开Git仓库根目录下名为.git/info/exclude的文件。您在此处添加的任何规则都不会被检入,并且只会忽略本地仓库的文件。
In Terminal, navigate to the location of your Git repository.
Using your favorite text editor, open the file.git/info/exclude.
.gitignore 文件时的格式
基本规范:
- 所有空行或者以注释符号
#开头的行都会被 Git 忽略。 - 匹配模式最后跟反斜杠
/说明要忽略的是目录 - 要
忽略指定模式以外的文件或目录,可以在模式前加上惊叹号!取反 - 可以使用标准的 glob 模式匹配
所谓的 glob 模式是指 shell 所使用的简化了的正则表达式:
- 星号
*匹配零个或多个任意字符 - 问号
?只匹配一个任意字符 - 方括号
[]匹配任何一个列在方括号中的字符 - 如果在方括号中使用短划线
-分隔两个字符,表示所有在这两个字符范围内的都可以匹配,如[0-9]表示匹配所有 0 到 9 的数字
Android 适用的 .gitignore
# Built application files
*.apk
*.ap_
*.aab
# Files for the ART/Dalvik VM
*.dex
# Java class files
*.class
# Generated files
bin/
gen/
out/
# Gradle files
.gradle/
build/
# Local configuration file (sdk path, etc)
local.properties
# Proguard folder generated by Eclipse
proguard/
# Log Files
*.log
# Android Studio Navigation editor temp files
.navigation/
# Android Studio captures folder
captures/
# IntelliJ
*.iml
.idea/workspace.xml
.idea/tasks.xml
.idea/gradle.xml
.idea/assetWizardSettings.xml
.idea/dictionaries
.idea/libraries
.idea/caches
# Keystore files
# Uncomment the following lines if you do not want to check your keystore files in.
#*.jks
#*.keystore
# External native build folder generated in Android Studio 2.2 and later
.externalNativeBuild
# Google Services (e.g. APIs or Firebase)
google-services.json
# Freeline
freeline.py
freeline/
freeline_project_description.json
# fastlane
fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots
fastlane/test_output
fastlane/readme.md
补充:让 Git 自动替换文件中的指定内容
第一步
- 在工程的根目录下创建/打开一个
.gitattributes文件,此文件会被提交到本地或者远程仓库。 - 或者在在工程
/.git/info/目录下创建attributes文件,此文件不会被提交到本地或者远程仓库。
第二步
在第一步的文件中添加如下内容,用于定义有部分内容需要被过滤或者忽略的文件:
*.txt filter=_config # 【.txt】代表要忽略的文件类型,【_config】代表此类型文件使用的过滤器
这表明针对所有的【txt】文件将要应用名为【_config】的过滤器。
第三步
在你的.gitignore里定义对应的过滤规则
3.1、我们需要在 push/add 代码时将【AAA】替换成【BBB】,我们可以利用sed指令这么配置:
git config --global filter._config.clean 'sed "s/AAA/BBB/g"'
其中【_config】与第二步中的filter名字匹配,sed指令实现了这种替换,整句会在 git add 时被触发。
注意:被替换的内容不能包含中文。
3.2、我们需要在 pull/checkout 代码的时候让服务端的【BBB】恢复为【AAA】,我们可以利用sed指令这么配置:
git config --global filter._config.smudge 'sed "s/BBB/AAA/g"'
和上面的基本一样,但是这次用的不是clean,而是smudge,它会在 git checkout 时被触发。
当然,你也可以直接在全局的.gitconfig或者项目下 .git/config 里面添加如下内容,其和前文两条指令等效:
[filter "_config"]
clean = sed \"s/AAA/BBB/g\"
smudge = sed \"s/BBB/AAA/g\"
smudge = sed \"s/CCC/AAA/g\"
smudge = sed \"s/DDD/AAA/g\"
另外,我们还可以将远端多个不同的值替换为某一个相同的值,例如:
[filter "_config"]
smudge = sed \"s/BBB/AAA/g\"
smudge = sed \"s/CCC/AAA/g\"
smudge = sed \"s/DDD/AAA/g\"
不过,实际上,因为功能有限(或者是我了解的还不够多),这玩意基本没啥卵用。
2017-11-7
Git 忽略规则 .gitignore文件 MD的更多相关文章
- Git忽略规则.gitignore梳理
对于经常使用Git的朋友来说,.gitignore配置一定不会陌生.废话不说多了,接下来就来说说这个.gitignore的使用. 首先要强调一点,这个文件的完整文件名就是".gitignor ...
- git忽略规则.gitignore未生效解决办法
创建git本地的仓库常用命令:git init #初始化--------生成.git文件 配置本地用户名跟邮箱(这样就知道是谁提交的东西)git config --global user.name [ ...
- Git忽略规则(.gitignore配置)不生效原因和解决
问题: .gitignore中已经标明忽略的文件目录下的文件,git push的时候还会出现在push的目录中,或者用git status查看状态,想要忽略的文件还是显示被追踪状态. 原因是因为在gi ...
- Git忽略规则.gitignore忽略node_modules文件夹
在项目文件夹里添加.gitignore的文件 打开文件,在里面添加 /node_modules
- Git忽略提交规则 .gitignore文件
在使用Git的过程中,我们喜欢有的文件比如日志,临时文件,编译的中间文件等不要提交到代码仓库,这时就要设置相应的忽略规则,来忽略这些文件的提交.简单来说一个场景:在你使用git add .的时候,遇到 ...
- git忽略规则以及.gitignore文件不生效解决办法
正文 Git忽略规则: #此为注释 – 内容被 Git 忽略 .sample # 忽略所有 .sample 结尾的文件 !lib.sample # 但 lib.sample 除外 /TODO # 仅仅 ...
- Git忽略规则及.gitignore规则不生效的解决办法
在git中如果想忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改根目录中 .gitignore 文件的方法(如无,则需自己手工建立此文件).这个文件每一行保存了一个匹配的规则例如: # 此为注 ...
- 谈谈Git的忽略规则.gitignore
对于经常使用Git的朋友来说,.gitignore配置一定不会陌生. 今天就来说说这个.gitignore的使用. 首先要强调一点,这个文件的完整文件名就是“.gitignore”,注意最前面有个“. ...
- Git忽略规则和.gitignore规则不生效的解决办法
Git忽略规则和.gitignore规则不生效的解决办法 Git忽略规则: 在git中如果想忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改根目录中 .gitignore 文件的方法(如果 ...
随机推荐
- 通过TortoiseGit上传项目到GitHub
1.安装msysgit和TortoiseGit : 2.TortoiseGit 设置: (1).确保安装成功: (2).设置用户名和邮箱: 3.登陆github并进入设置页面: 4.添加 SSH Ke ...
- LCT维护子树信息
有些题目,在要求支持link-cut之外,还会在线询问某个子树的信息.LCT可以通过维护虚边信息完成这个操作. 对于LCT上每个节点,维护两个两sz和si,后者维护该点所有虚儿子的信息,前者维护该点的 ...
- bzoj 4408: [Fjoi 2016]神秘数 数学 可持久化线段树 主席树
https://www.lydsy.com/JudgeOnline/problem.php?id=4299 一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数.例如S={1,1,1 ...
- 洛谷.4238.[模板]多项式求逆(NTT)
题目链接 设多项式\(f(x)\)在模\(x^n\)下的逆元为\(g(x)\) \[f(x)g(x)\equiv 1\ (mod\ x^n)\] \[f(x)g(x)-1\equiv 0\ (mod\ ...
- java集合系列之三(ArrayList)
上一章,我们学习了Collection的架构.这一章开始,我们对Collection的具体实现类进行讲解:首先,讲解List,而List中ArrayList又最为常用.因此,本章我们讲解ArrayLi ...
- 在MacBook下安装http链接的性能测试工具httping
一.如果没有brew,先安装brew. 安装命令如下:curl -LsSf http://github.com/mxcl/homebrew/tarball/master | sudo tar xvz ...
- python3登录网页(163邮箱)实例
# -*- coding: utf-8 -*- import urllibimport http.cookiejar as cookielibimport urllib.request as urll ...
- An ac a day,keep wa away
zoj 初学者题: 1001 1037 1048 1049 1051 1067 1115 1151 1201 1205 1216 1240 1241 1242 1251 1292 1331 1334 ...
- Windows下openssl安装及使用
配置过程中需要生成一些mak文件,这些生成代码用perl脚本生成,所以要安装一个ActivePerl. 网址: http://www.activestate.com/activeperl/ 下载后直接 ...
- sql 递归查询所有的下级
--> 生成测试数据表: [tb] IF OBJECT_ID('[Users]') IS NOT NULL DROP TABLE [Users] GO CREATE TABLE [Use ...