Git 忽略規則 .gitignore文件 MD

Markdown版本筆記 個人GitHub首頁 個人博客 個人微信 個人郵箱
MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina.com

Git 忽略規則 .gitignore文件 MDhtml


目錄

添加忽略規則的三種方式

添加忽略規則 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忽略哪些文件。github

局部 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文件應該被提交到你的倉庫中,以便與任何其餘克隆倉庫的用戶共享這個忽略規則。shell

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文件。json

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.app

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中建立一個文件,併爲其添加一些規則。ide

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.一、咱們須要在 push/add 代碼時將【AAA】替換成【BBB】,咱們能夠利用sed指令這麼配置:

git config --global filter._config.clean 'sed "s/AAA/BBB/g"'

其中【_config】與第二步中的filter名字匹配,sed指令實現了這種替換,整句會在 git add 時被觸發。
注意:被替換的內容不能包含中文。

3.二、咱們須要在 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

相關文章
相關標籤/搜索