代码之家  ›  专栏  ›  技术社区  ›  neoneye

忽略的Git diff ^m

  •  368
  • neoneye  · 技术社区  · 15 年前

    在某些文件包含^m作为换行符的项目中。区分这些文件显然是不可能的,因为GitDiff认为整个文件只是一行。

    与以前的版本有什么不同?

    是否有“区分时将^m视为换行”这样的选项?

    prompt> git-diff "HEAD^" -- MyFile.as 
    diff --git a/myproject/MyFile.as b/myproject/MyFile.as
    index be78321..a393ba3 100644
    --- a/myproject/MyFile.cpp
    +++ b/myproject/MyFile.cpp
    @@ -1 +1 @@
    -<U+FEFF>import flash.events.MouseEvent;^Mimport mx.controls.*;^Mimport mx.utils.Delegate
    \ No newline at end of file
    +<U+FEFF>import flash.events.MouseEvent;^Mimport mx.controls.*;^Mimport mx.utils.Delegate
    \ No newline at end of file
    prompt>
    

    更新:

    现在我写了一个脚本,检查最新的10个版本,并将CR转换为LF。

    require 'fileutils'
    
    if ARGV.size != 3
      puts "a git-path must be provided"
      puts "a filename must be provided"
      puts "a result-dir must be provided"
      puts "example:"
      puts "ruby gitcrdiff.rb project/dir1/dir2/dir3/ SomeFile.cpp tmp_somefile"
      exit(1)
    end
    
    gitpath = ARGV[0]
    filename = ARGV[1]
    resultdir = ARGV[2]
    
    unless FileTest.exist?(".git")
      puts "this command must be run in the same dir as where .git resides"
      exit(1)
    end
    
    if FileTest.exist?(resultdir)
      puts "the result dir must not exist"
      exit(1)
    end
    FileUtils.mkdir(resultdir)
    
    10.times do |i|
      revision = "^" * i
      cmd = "git show HEAD#{revision}:#{gitpath}#{filename} | tr '\\r' '\\n' > #{resultdir}/#{filename}_rev#{i}"
      puts cmd 
      system cmd
    end
    
    9 回复  |  直到 7 年前
        1
  •  303
  •   random    9 年前

    GitHub suggests 您应该确保在git handled repos中只使用\n作为换行符。有一个自动转换选项:

    $ git config --global core.autocrlf true
    

    当然,这是说转换cr lf到lf,而你想转换cr到lf。我希望这仍然有效

    然后转换文件:

    # Remove everything from the index
    $ git rm --cached -r .
    
    # Re-add all the deleted files to the index
    # You should get lots of messages like: "warning: CRLF will be replaced by LF in <file>."
    $ git diff --cached --name-only -z | xargs -0 git add
    
    # Commit
    $ git commit -m "Fix CRLF"
    

    core.autocrlf描述于 the man page .

        2
  •  300
  •   Ryan Lundy    13 年前

    在Windows上开发时,我在使用时遇到了这个问题 git tfs . 我是这样解决的:

    git config --global core.whitespace cr-at-eol
    

    这基本上告诉Git,行CR的结尾不是错误。结果,那些烦人的 ^M 中的行尾不再出现字符 git diff , git show 等。

    它似乎保留了其他设置;例如,行末尾的额外空格在diff中仍显示为错误(以红色突出显示)。

    (其他答案也提到了这一点,但上面的内容正是如何设置设置的。若要仅为一个项目设置设置,请忽略 --global )

    编辑 :

    在经历了许多行不通的痛苦之后,当我在一个.NET团队中工作时,我获得了最好的运气,使用以下设置:

    • 无CORE.EOL设置
    • 没有core.whitespace设置
    • 无core.autocrlf设置
    • 运行用于Windows的Git安装程序时,您将获得以下三个选项:
      • 签出Windows样式,提交Unix样式的行尾 <--选择这个
      • 按原样签出,提交Unix样式的行尾
      • 按原样签出,按原样提交

    如果需要使用空白设置,那么如果需要与tfs交互,那么可能只应在每个项目的基础上启用它。只是省略了 --全球 :

    git config core.whitespace cr-at-eol
    

    如果需要删除一些core.*设置,最简单的方法是运行此命令:

    git config --global -e
    

    这将在文本编辑器中打开global.gitconfig文件,您可以轻松删除要删除的行。(或者你也可以在他们面前加上“”,把他们说出来。)

        3
  •  108
  •   Jakub Narębski adamtaub    15 年前

    尝试 git diff --ignore-space-at-eol git diff --ignore-space-change ,或 git diff --ignore-all-space .

        4
  •  90
  •   Rufflewind    10 年前

    还可以看到:

    core.whitespace = cr-at-eol
    

    或者相当地,

    [core]
        whitespace = cr-at-eol
    

    在哪里? whitespace 前面是 标签 性格。

        5
  •  36
  •   gitaarik    8 年前

    你为什么要这些 ^M 在你 git diff ?

    在我的案例中,我正在开发一个在Windows中开发的项目,我使用了OSX。当我更改了一些代码时,我看到 ^ m 在我加入的行尾 差异比较 . 我认为 ^ m 出现是因为它们的行尾与文件的其他行尾不同。因为文件的其余部分是在Windows中开发的 CR 行尾,在OS X中使用 LF 行结束。

    显然,Windows开发人员没有使用该选项“ 签出Windows样式,提交Unix样式的行尾 “安装Git时。

    那我们该怎么办呢?

    您可以让Windows用户重新安装Git并使用“ 签出Windows样式,提交Unix样式的行尾 “选项。这是我更喜欢的,因为我把Windows看作是一个例外,它的行尾字符和Windows修复它自己的问题这样。

    如果选择此选项,则应该修复当前文件(因为它们仍在使用 行结束)。我是通过以下步骤来做到这一点的:

    1. 从存储库中删除所有文件,但不要从文件系统中删除。

      git rm --cached -r .
      
    2. 添加一个 .gitattributes 强制某些文件使用 低频 作为行尾。将此放入文件:

      *.ext text eol=crlf
      

      替换 .ext 与要匹配的文件扩展名匹配。

    3. 重新添加所有文件。

      git add .
      

      这将显示如下消息:

      warning: CRLF will be replaced by LF in <filename>.
      The file will have its original line endings in your working directory.
      
    4. 你可以移除 GITS属性 文件,除非您有顽固的Windows用户不想使用“ 签出Windows样式,提交Unix样式的行尾 “期权。

    5. 全力以赴。

    6. 删除并签出所有使用它们的系统上的适用文件。在Windows系统上,确保他们现在使用“ 签出Windows样式,提交Unix样式的行尾 “期权。在执行这些任务的系统上也应该这样做,因为添加文件时,git说:

      The file will have its original line endings in your working directory.
      

      您可以这样做删除文件:

      git ls | grep ".ext$" | xargs rm -f
      

      然后用正确的行尾将它们返回:

      git ls | grep ".ext$" | xargs git checkout
      

      当然换了 Ext 使用您想要的扩展名。

    现在您的项目只使用 低频 行尾的字符和讨厌的 角色永远不会回来的:)。

    另一个选项是强制使用Windows样式的行尾。您也可以使用 GITS属性 文件。

    更多信息: https://help.github.com/articles/dealing-with-line-endings/#platform-all

        6
  •  19
  •   kaartic JoshDM    7 年前

    是否有“区分时将^m视为换行”这样的选项?

    将有一个2.16吉特(2018年第一季度)作为 diff “一系列的命令学会了忽略行尾的回车差异。

    commit e9282f0 (2017年10月26日) Junio C Hamano ( gitster ) .
    帮助: Johannes Schindelin ( dscho ) .
    (合并) Junio C Hamano -- gitster -- 在里面 commit 10f65c2 2017年11月27日)

    微分: --ignore-cr-at-eol

    一种新的选择 --在EOL时忽略CR 告诉diff machinery将(完整)行末尾的回车视为不存在。

    就像其他人一样” --ignore-* “忽略各种空白差异的选项,这将有助于检查您所做的实际更改,而不会被虚假的干扰。 CRLF<->LF 由编辑器程序进行的转换。

        7
  •  13
  •   Jason Pyeron Evgeniy Dorofeev    7 年前

    DR

    改变 core.pager "tr -d '\r' | less -REX" ,而不是源代码

    这就是为什么

    显示的那些讨厌的^M是彩色化和寻呼机的产物。 enter image description here 它是由 less -R ,默认的Git寻呼机选项。(Git的默认寻呼机是 less -REX )

    首先要注意的是 git diff -b 不会显示空白中的更改(例如\r\n与\n)

    设置:

    git clone https://github.com/CipherShed/CipherShed
    cd CipherShed
    

    创建Unix文件并更改行尾的快速测试不会显示 GIT-DIF-B :

    echo -e 'The quick brown fox\njumped over the lazy\ndogs.' > test.txt
    git add test.txt
    unix2dos.exe test.txt
    git diff -b test.txt
    

    我们注意到,强制将管道缩小并不显示^m,而是启用颜色和 少-r 做:

    git diff origin/v0.7.4.0 origin/v0.7.4.1 | less
    git -c color.ui=always diff origin/v0.7.4.0 origin/v0.7.4.1 | less -R
    

    通过使用管道从输出中剥离\r(^m)来显示修复:

    git diff origin/v0.7.4.0 origin/v0.7.4.1
    git -c core.pager="tr -d '\r' | less -REX"  diff origin/v0.7.4.0 origin/v0.7.4.1
    

    一个不明智的选择是使用 less -r ,因为它将通过所有控制代码,而不仅仅是颜色代码。

    如果只想直接编辑Git配置文件,这是要更新/添加的条目:

    [core]
            pager = tr -d '\\r' | less -REX
    
        8
  •  10
  •   Ian Wojtowicz    13 年前

    我在这个问题上挣扎了很长时间。到目前为止,最简单的解决方案是不用担心^M字符,只需使用一个可以处理它们的可视diff工具。

    而不是键入:

    git diff <commitHash> <filename>
    

    尝试:

    git difftool <commitHash> <filename>
    
        9
  •  0
  •   Adil    9 年前

    如果您使用Eclipse,您可以 ^M 从…消失 git diff 通过设置 File > Convert Line Delimiter To > Unix (LF, \n, 0A, ¶)

    推荐文章