在项目中,其中一些文件包含^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
为什么要在git diff中加入^M ?
以我为例,我正在开发一个在Windows上开发的项目,我使用的是Linux。当我修改一些代码时,我在git diff中添加的行末尾看到^M。我认为^M出现是因为它们与文件的其他部分不同的行结束符。因为文件的其余部分是在Windows上开发的,所以它使用CRLF行结束符,而在Linux中它使用LF行结束符。
显然,Windows开发人员在安装Git时没有使用“结帐Windows风格,提交unix风格的行尾”选项。
那么我们该怎么做呢?
你可以让Windows用户重新安装git并使用“Checkout Windows style, commit unix style line ending”选项。这是我更喜欢的,因为我认为Windows在行尾字符方面是个例外,Windows用这种方式解决了自己的问题。
如果选择此选项,则应该修复当前文件(因为它们仍在使用CRLF行结束符)。我是通过以下步骤做到的:
Remove all files from the repository, but not from your filesystem.
git rm --cached -r .
Add a .gitattributes file that enforces certain files to use a LF as line endings. Put this in the file:
* text=auto eol=lf
Add all the files again.
git add .
This will show messages like this:
warning: CRLF will be replaced by LF in <filename>.
The file will have its original line endings in your working directory.
You could remove the .gitattributes file unless you have stubborn Windows users that don't want to use the "Checkout Windows-style, commit Unix-style line endings" option.
Commit and push it all.
Remove and checkout the applicable files on all the systems where they're used. On the Windows systems, make sure they now use the "Checkout Windows-style, commit Unix-style line endings" option. You should also do this on the system where you executed these tasks because when you added the files git said:
The file will have its original line endings in your working directory.
You can do something like this to remove the files:
git ls | grep ".ext$" | xargs rm -f
And then this to get them back with the correct line endings:
git ls | grep ".ext$" | xargs git checkout
Replacing .ext with the file extensions you want to match.
现在你的项目只使用LF字符作为行结束,讨厌的CR字符将永远不会回来:)。
另一种选择是强制windows样式的行结束符。您还可以为此使用.gitattributes文件。
更多信息:
https://help.github.com/articles/dealing-with-line-endings/#platform-all