zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Win8 iis 环境搭建
    Windows phone 8 触发器使用小结
    Windows Phone 页面之间参数传递方法
    日期SQL 脚本
    net 内存泄露和内存溢出
    Emacs的一些事情(与Vi的争议及使用)
    matlab与示波器连接及电脑连接
    msp430学习笔记-TA
    28个Unix/Linux的命令行神器
    linux在线中文手册
  • 原文地址:https://www.cnblogs.com/baiqiantao/p/7798132.html
Copyright © 2011-2022 走看看