【问题标题】:Git ignore trailing whitespace in markdown files onlyGit 仅忽略 Markdown 文件中的尾随空格
【发布时间】:2017-02-06 18:00:30
【问题描述】:

我有一个降价文件,其中的行带有尾随空格(这是正确的,应该提交)。我无法使用 git add -p 将这些更改添加到索引中,因为 git 抱怨尾随空格。如果我使用 git add -A,它们会被正确添加,但我希望它与 git add -p 一起使用。

我在我的~/.gitconfig:

[core]
  whitespace = trailing-space,space-before-tab

这一直运行良好,因为在大多数情况下我确实想警告尾随空格(它在 HTML、JS 和 Ruby 文件中不正确)。

如何仅忽略 Markdown 文件中的尾随空格?

【问题讨论】:

    标签: git markdown whitespace trailing


    【解决方案1】:

    .gitattributes 文件中添加以下内容:

    **/*.md  -whitespace
    

    https://git-scm.com/docs/gitattributes#_checking_whitespace_errors

    更具体地说,您可以改为执行以下操作:

    **/*.md  whitespace=space-before-tab
    

    (为降价文件删除trailing-space。)

    以与 .gitignore 相同的方式处理 .gitattributes 并将其签入 repo。

    【讨论】:

    • **/* 是什么意思? -whitespace 表示“取消设置 whitespace 属性”。
    【解决方案2】:

    .gitattributes中使用这个:

    **/*.md text whitespace=-cr-at-eol,-trailing-space
    

    **/*.md whitespace=space-before-tab 不起作用。在cmd.exe 试试这个:

    $ git config --show-origin --get core.whitespace
    file:C:/Users/kevin/.gitconfig  trailing-space,space-before-tab,cr-at-eol
    
    $ git init .
    Initialized empty Git repository in trailing/.git/
    
    $ cat > README.md
    Trailing space here:
    check it
    
    $ git add README.md
    
    $ git diff-index --check --cached 4b825dc642cb6eb9a060e54bf8d69288fbee4904
    README.md:1: trailing whitespace.
    +Trailing space here: 
    
    $ echo **/*.md  -whitespace > .gitattributes
    
    $ git check-attr --all -- README.md
    README.md: whitespace: unset
    
    $ git diff-index --check --cached 4b825dc642cb6eb9a060e54bf8d69288fbee4904
    
    $ echo **/*.md  whitespace=space-before-tab > .gitattributes
    
    $ git check-attr --all -- README.md
    README.md: whitespace: space-before-tab
    
    $ git diff-index --check --cached 4b825dc642cb6eb9a060e54bf8d69288fbee4904
    README.md:1: trailing whitespace.
    +Trailing space here:
    
    $ echo **/*.md text whitespace=-cr-at-eol,-trailing-space > .gitattributes
    
    $ git diff-index --check --cached 4b825dc642cb6eb9a060e54bf8d69288fbee4904
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-20
      • 2013-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多