Git 忽略规则 .gitignore文件
Posted 白乾涛
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Git 忽略规则 .gitignore文件相关的知识,希望对你有一定的参考价值。
添加忽略规则的三种方式
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在 github/gitignore 公共仓库中维护了一个官方的列表,(列表里面包含了在使用)许多流行的操作系统、环境和语言时的推荐的.gitignore文件。
- In Terminal, navigate to the location of your Git repository.
- Enter touch .gitignore to create a .gitignore file.
touch .gitignore
1
1
1
touch .gitignore
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 # 停止追踪指定文件,但该文件会保留在工作区
1
1
1
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
1
1
1
git config --global core.excludesfile ~/.gitignore_global
个人 Explicit repository excludes
explicit [?k?spl?s?t] adj. 明确的,清楚的; 直言的; 详述的; 不隐瞒的;
exclude [?k‘sklu:d] vt. 排斥;排除,不包括;驱除,赶出
2
2
1
explicit [?k?spl?s?t] adj. 明确的,清楚的; 直言的; 详述的; 不隐瞒的;
2
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.
GitHub中的.gitignore项目
A collection of .gitignore templates .gitignore模板的集合
This is GitHub’s collection of .gitignore file templates. We use this list to populate the .gitignore template choosers available in the GitHub.com interface when creating new repositories and files.
这是GitHub的.gitignore文件模板的集合。当在GitHub.com中创建新的仓库和文件时,我们使用这个列表来填充可用的.gitignore模板选择器。
For more information about how .gitignore files work, and how to use them, the following resources are a great place to start:
- The Ignoring Files chapter of the Pro Git book.
- The Ignoring Files article on the GitHub Help site.
- The gitignore(5) manual page.
【文件夹结构 Folder structure】
The files in the root directory are for .gitignore templates that are project specific, such as language or framework specific templates. Global (operating system or editor specific) templates should go into the Global/ directory.
根目录中的文件用于特定于项目的.gitignore模板,如语言或特定于框架的模板。全局(操作系统或编辑器特定)模板应该去【Global/】目录中去找。
【贡献指南 Contributing guidelines】
We’d love for you to help us improve this project. To help us keep this collection high quality, we request that contributions adhere to the following guidelines.
我们很乐意您能帮助我们改善这个项目。为了帮助我们保持这个集合的高质量,我们要求贡献者遵守以下准则。
后面省略一千字...
android.gitignore
# Built application files
*.apk
*.ap_
# 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/dictionaries
.idea/libraries
# Keystore files
# Uncomment the following line if you do not want to check your keystore files in.
#*.jks
# 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
1
2
*.apk
3
*.ap_
4
5
6
*.dex
7
8
9
*.class
10
11
12
bin/
13
gen/
14
out/
15
16
17
.gradle/
18
build/
19
20
21
local.properties
22
23
24
proguard/
25
26
27
*.log
28
29
30
.navigation/
31
32
33
captures/
34
35
36
*.iml
37
.idea/workspace.xml
38
.idea/tasks.xml
39
.idea/gradle.xml
40
.idea/dictionaries
41
.idea/libraries
42
43
44
45
46
47
48
.externalNativeBuild
49
50
51
google-services.json
52
53
54
freeline.py
55
freeline/
56
freeline_project_description.json
Java.gitignore
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
22
1
# Compiled class file
2
*.class
3
4
# Log file
5
*.log
6
7
# BlueJ files
8
*.ctxt
9
10
# Mobile Tools for Java (J2ME)
11
.mtj.tmp/
12
13
# Package Files #
14
*.jar
15
*.war
16
*.ear
17
*.zip
18
*.tar.gz
19
*.rar
20
21
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
22
hs_err_pid*
2017-11-7
以上是关于Git 忽略规则 .gitignore文件的主要内容,如果未能解决你的问题,请参考以下文章