在项目中,其中一些文件包含^M作为换行符 分隔器,区分这些文件显然是不可能的,因为 Git diff将整个文件视为一行。

比较当前和以前的git有什么不同 源代码文件的版本?

有一个选项,如“对待^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>

更新:

我写了一个Ruby脚本来检查最新的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

当前回答

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

而不是打字:

git diff <commitHash> <filename>

try:

git difftool <commitHash> <filename>

其他回答

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

而不是打字:

git diff <commitHash> <filename>

try:

git difftool <commitHash> <filename>

还看到:

core.whitespace = cr-at-eol

或者说,

[core]
    whitespace = cr-at-eol

空格前有制表符。

试试git diff——ignore-space-at-eol,或者git diff——ignore-space-change,或者git diff——ignore-all-space。

在我的例子中,这是一个命令:

git config  core.whitespace cr-at-eol

来源:https://public inbox.org/git/8d7e4807 - 9 -你姓名e357 - 8265 - 95 f22ab716e0@web.de / T /

有一个选项,如“对待^M换行时,差异”?

Git 2.16(2018年第一季度)将会出现一个问题,因为“diff”命令家族学会了忽略行末回车中的差异。

参见Junio C Hamano (gitster)提交e9282f0(2017年10月26日)。 帮助:Johannes Schindelin (dscho)。 (由Junio C Hamano—gitster—在commit 10f65c2中合并,2017年11月27日)

diff: ignore-cr-at-eol 一个新的选项——ignore-cr-at-eol告诉diff机器在(完整的)行末尾处理回车,就像它不存在一样。 就像其他“——ignore-*”选项忽略各种空白差异一样,这将有助于检查您所做的实际更改,而不会被编辑器程序所做的虚假CRLF<->LF转换分心。