Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱
MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina.com

Git 忽略规则 .gitignore文件 MD


目录

添加忽略规则的三种方式

添加忽略规则

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.

Enter touch .gitignore to 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

.gitignore项目

Android忽略文件

# 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的更多相关文章

  1. Git忽略规则.gitignore梳理

    对于经常使用Git的朋友来说,.gitignore配置一定不会陌生.废话不说多了,接下来就来说说这个.gitignore的使用. 首先要强调一点,这个文件的完整文件名就是".gitignor ...

  2. git忽略规则.gitignore未生效解决办法

    创建git本地的仓库常用命令:git init #初始化--------生成.git文件 配置本地用户名跟邮箱(这样就知道是谁提交的东西)git config --global user.name [ ...

  3. Git忽略规则(.gitignore配置)不生效原因和解决

    问题: .gitignore中已经标明忽略的文件目录下的文件,git push的时候还会出现在push的目录中,或者用git status查看状态,想要忽略的文件还是显示被追踪状态. 原因是因为在gi ...

  4. Git忽略规则.gitignore忽略node_modules文件夹

    在项目文件夹里添加.gitignore的文件 打开文件,在里面添加 /node_modules

  5. Git忽略提交规则 .gitignore文件

    在使用Git的过程中,我们喜欢有的文件比如日志,临时文件,编译的中间文件等不要提交到代码仓库,这时就要设置相应的忽略规则,来忽略这些文件的提交.简单来说一个场景:在你使用git add .的时候,遇到 ...

  6. git忽略规则以及.gitignore文件不生效解决办法

    正文 Git忽略规则: #此为注释 – 内容被 Git 忽略 .sample # 忽略所有 .sample 结尾的文件 !lib.sample # 但 lib.sample 除外 /TODO # 仅仅 ...

  7. Git忽略规则及.gitignore规则不生效的解决办法

    在git中如果想忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改根目录中 .gitignore 文件的方法(如无,则需自己手工建立此文件).这个文件每一行保存了一个匹配的规则例如: # 此为注 ...

  8. 谈谈Git的忽略规则.gitignore

    对于经常使用Git的朋友来说,.gitignore配置一定不会陌生. 今天就来说说这个.gitignore的使用. 首先要强调一点,这个文件的完整文件名就是“.gitignore”,注意最前面有个“. ...

  9. Git忽略规则和.gitignore规则不生效的解决办法

    Git忽略规则和.gitignore规则不生效的解决办法   Git忽略规则: 在git中如果想忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改根目录中 .gitignore 文件的方法(如果 ...

随机推荐

  1. linux 下安装jdk环境安装

    一.创建jdk安装目录mkdir /usr/local/java 二.将jdk解压到安装目录中,直接到java目录中,如果不是处理下不要有子目录 tar -zxvf jdk-8u91-linux-x6 ...

  2. CSUOJ 1808 地铁

    Description Bobo 居住在大城市 ICPCCamp. ICPCCamp 有 n 个地铁站,用 1,2,-,n 编号. m 段双向的地铁线路连接 n 个地铁站,其中第 i 段地铁属于 ci ...

  3. C++实现平衡二叉树

    1.概念 平衡二叉树(AVL Tree)首先要满足二叉树的定义,如下 二叉排序树或者是一棵空树,或者是具有下列性质的二叉树: 若左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若右子树不空, ...

  4. Django配置参数可选总结

    一.可选字段参数 null blank core db_index editable primary_key radio_admin unique True or False db_colum hel ...

  5. Python Django 中的STATIC_URL 设置和使用解析

    使用Django静态设置时,遇到很多问题,经过艰苦的Baidu, stack overflow, Django原档阅读,终于把静态图片给搞出来了.特记录下来. 关键的概念:Django中,静态资源的存 ...

  6. Codeforces Round #502 (in memory of Leopoldo Taravilse, Div. 1 + Div. 2) G. The Tree

    G. The Tree time limit per test 3 seconds memory limit per test 256 megabytes input standard input o ...

  7. 鸟哥的私房菜:Bash shell(四)-Bash shell的使用环境

    Bash shell(四)-Bash shell的使用环境   是否记得我们登入主机的时候,屏幕上头会有一些说明文字,告知我们的 Linux 版本啊什么的, 还有,登入的时候,我们还可以给予使用者一些 ...

  8. hdu 2732 最大流 **

    题意:题目是说一个n*m的迷宫中,有每个格子有柱子.柱子高度为0~3,高度为0的柱子是不能站的(高度为0就是没有柱子)在一些有柱子的格子上有一些蜥蜴,一次最多跳距离d,相邻格子的距离是1,只要跳出迷宫 ...

  9. 开源中国上抓取的content-type

    开源中国上抓取的content-type类型,来源:http://www.cnblogs.com/smallyard/p/5632608.html { ".*": "ap ...

  10. the elements of computing systems 的读书笔记2

    懒癌发作,本来计划是两到三天就一个unit的,没想到一直拖到今天才完成第二部分(6-8章). 第6章,介绍了hack汇编到二进制,也就是用翻译到01来表示.从课后习题来看,这一章目的就是设计一个程序( ...