neo*_*eye 423 git diff newline git-diff
在某个项目中,某些文件包含^ M作为换行符分隔符.分散这些文件显然是不可能的,因为git-diff认为它是整个文件只是一行.
如何与之前的版本区别开来?
有没有像"在分辨时将^ 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>
Run Code Online (Sandbox Code Playgroud)
更新:
现在我编写了一个脚本,检查最新的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
Run Code Online (Sandbox Code Playgroud)
nes*_*983 352
GitHub建议您确保只使用\n作为git-handling repos中的换行符.有一个自动转换选项:
$ git config --global core.autocrlf true
Run Code Online (Sandbox Code Playgroud)
当然,据说将crlf转换为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"
Run Code Online (Sandbox Code Playgroud)
core.autocrlf 在手册页中描述.
Rya*_*ndy 346
在Windows上开发,我在使用时遇到了这个问题git tfs.我这样解决了:
git config --global core.whitespace cr-at-eol
Run Code Online (Sandbox Code Playgroud)
这基本上告诉Git,行尾CR不是错误.其结果是,那些烦人的^M字符不再出现在线路末端git diff,git show等等.
它似乎按原样保留其他设置; 例如,一行末尾的额外空格仍显示为差异中的错误(以红色突出显示).
(其他答案已经提到了这一点,但以上就是如何设置设置.要设置仅一个项目的设置,请省略--global.)
编辑:
在经历了许多线路结束的艰辛之后,在使用这些设置时,我在.NET团队工作时运气最好:
如果需要使用空白设置,如果需要与TFS交互,则应该仅在每个项目的基础上启用它.只需省略--global:
git config core.whitespace cr-at-eol
Run Code Online (Sandbox Code Playgroud)
如果您需要删除一些核心.*设置,最简单的方法是运行此命令:
git config --global -e
Run Code Online (Sandbox Code Playgroud)
这将在文本编辑器中打开您的全局.gitconfig文件,您可以轻松删除要删除的行.(或者你可以在他们面前加上'#'来评论它们.)
Jak*_*ski 114
尝试git diff --ignore-space-at-eol,或git diff --ignore-space-change,或git diff --ignore-all-space.
Vla*_*eev 101
另见:
core.whitespace = cr-at-eol
Run Code Online (Sandbox Code Playgroud)
或等效地,
[core]
whitespace = cr-at-eol
Run Code Online (Sandbox Code Playgroud)
其中whitespace以制表符为前缀.
git*_*rik 40
^M在你的git diff?在我的情况下,我正在开发一个在Windows中开发的项目,我使用OS X.当我更改了一些代码时,我看到^M了我添加的行的末尾git diff.我认为^M它们出现了,因为它们的行结尾与文件的其余部分不同.因为文件的其余部分是在Windows中开发的,所以它使用CR行结尾,而在OS X中则使用LF行结尾.
显然,Windows开发人员在安装Git期间没有使用" Checkout Windows风格,提交Unix风格的行结尾 " 选项.
您可以让Windows用户重新安装git并使用" Checkout Windows风格,提交Unix风格的行结尾 "选项.这是我更喜欢的,因为我认为Windows是一个例外,它的行结束字符和Windows通过这种方式修复了它自己的问题.
如果你选择这个选项,你应该修复当前文件(因为它们仍然使用CR行结尾).我是按照以下步骤完成的:
从存储库中删除所有文件,但不从文件系统中删除.
git rm --cached -r .
Run Code Online (Sandbox Code Playgroud)添加.gitattributes强制某些文件的文件以使用LFas行结尾.把它放在文件中:
*.ext text eol=crlf
Run Code Online (Sandbox Code Playgroud)
替换.ext为您要匹配的文件扩展名.
再次添加所有文件.
git add .
Run Code Online (Sandbox Code Playgroud)
这将显示如下消息:
warning: CRLF will be replaced by LF in <filename>.
The file will have its original line endings in your working directory.
Run Code Online (Sandbox Code Playgroud)您可以删除该.gitattributes文件,除非您有顽固的Windows用户不想使用" Checkout Windows风格,提交Unix风格的行结尾 "选项.
承诺并全力以赴.
删除并签出所有使用它们的系统上的适用文件.在Windows系统上,确保它们现在使用" Checkout Windows风格,提交Unix风格的行结尾 "选项.您还应该在执行这些任务的系统上执行此操作,因为当您添加文件时git说:
The file will have its original line endings in your working directory.
Run Code Online (Sandbox Code Playgroud)
您可以执行以下操作来删除文件:
git ls | grep ".ext$" | xargs rm -f
Run Code Online (Sandbox Code Playgroud)
然后用正确的行结尾让它们回来:
git ls | grep ".ext$" | xargs git checkout
Run Code Online (Sandbox Code Playgroud)
当然要替换.ext你想要的扩展名.
现在你的项目只使用LF行结尾的CR字符,而讨厌的字符将永远不会回来:).
另一种选择是强制执行Windows样式行结尾.您也可以使用该.gitattributes文件.
更多信息:https: //help.github.com/articles/dealing-with-line-endings/#platform-all
Von*_*onC 33
有没有像"在分辨时将^ M视为换行符"的选项?
将有一个Git 2.16(2018年第一季度),作为" diff"系列命令学会忽略行尾的回车差异.
请参阅Junio C Hamano()提交的e9282f0(2017年10月26日).
帮助:Johannes Schindelin().(由Junio C Hamano合并- -在提交10f65c2,2017年11月27日)gitsterdscho
gitster
DIFF:
--ignore-cr-at-eol一个新选项
--ignore-cr-at-eol告诉diff机器在(完整)行的末尾处理一个回车,好像它不存在一样.就像
--ignore-*忽略各种空白差异的其他" 选项" 一样,这将有助于回顾您所做的真正更改,而不会被CRLF<->LF编辑器程序的虚假转换分散注意力.
Jas*_*ron 20
TL; DR
更改core.pager为"tr -d '\r' | less -REX",而不是源代码
这就是为什么
显示的那些讨厌的^ M是着色和寻呼机的人工制品.
它是由less -R默认的git pager选项引起的.(git的默认寻呼机是less -REX)
首先要注意的是,git diff -b不会显示空白区域的变化(例如\ r \n vs \n \n)
建立:
git clone https://github.com/CipherShed/CipherShed
cd CipherShed
Run Code Online (Sandbox Code Playgroud)
创建unix文件并更改行结尾的快速测试将显示以下内容git diff -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
Run Code Online (Sandbox Code Playgroud)
我们注意到强制管道减少不显示^ M,但启用颜色并less -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
Run Code Online (Sandbox Code Playgroud)
通过使用管道从输出中剥离\ 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
Run Code Online (Sandbox Code Playgroud)
一个不明智的选择是使用less -r,因为它将通过所有控制代码,而不仅仅是颜色代码.
如果您只想直接编辑git配置文件,这是更新/添加的条目:
[core]
pager = tr -d '\\r' | less -REX
Run Code Online (Sandbox Code Playgroud)
小智 16
就我而言,这是什么命令:
git config core.whitespace cr-at-eol
Run Code Online (Sandbox Code Playgroud)
来源:https : //public-inbox.org/git/8d7e4807-9a79-e357-8265-95f22ab716e0@web.de/T/
小智 12
我很长一段时间都在努力解决这个问题.到目前为止,最简单的解决方案是不要担心^ M字符,只需使用可以处理它们的视觉差异工具.
而不是输入:
git diff <commitHash> <filename>
Run Code Online (Sandbox Code Playgroud)
尝试:
git difftool <commitHash> <filename>
Run Code Online (Sandbox Code Playgroud)