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
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.
Entertouch .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 # 中止追蹤指定文件,但該文件會保留在工做區
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 [ɪ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
.
基本規範:
#
開頭的行都會被 Git 忽略。 /
說明要忽略的是目錄
忽略
指定模式之外的文件或目錄,能夠在模式前加上驚歎號!
取反所謂的 glob 模式是指 shell 所使用的簡化了的正則表達式:
*
匹配零個或多個
任意字符?
只匹配一個
任意字符[]
匹配任何一個
列在方括號中的字符-
分隔兩個字符,表示全部在這兩個字符範圍內的均可以匹配,如[0-9]
表示匹配全部 0 到 9 的數字# 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
第一步
.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