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