Git 忽略規則 .gitignore文件 MD


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM