亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

git-diff忽略^ M

git-diff忽略^ M

Git
皈依舞 2019-09-27 16:31:53
在某些文件包含^ M作為換行符的項目中。顯然不可能對這些文件進行區分,因為git-diff將其視為整個文件僅一行。與以前的版本有何不同?是否有類似“比較時將^ M作為換行符”的選項?prompt> git-diff "HEAD^" -- MyFile.as diff --git a/myproject/MyFile.as b/myproject/MyFile.asindex 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 fileprompt>更新:現在,我編寫了一個腳本,用于檢查最新的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)endgitpath = 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)endif FileTest.exist?(resultdir)  puts "the result dir must not exist"  exit(1)endFileUtils.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 cmdend
查看完整描述

3 回答

?
慕娘9325324

TA貢獻1783條經驗 獲得超4個贊

GitHub建議您確保在git處理的存儲庫中僅使用\ n作為換行符。有一個自動轉換選項:


$ git config --global core.autocrlf true

當然,據說這是將crlf轉換為lf,而您想將crlf轉換為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 。


查看完整回答
反對 回復 2019-09-27
?
江戶川亂折騰

TA貢獻1851條經驗 獲得超5個贊

在Windows上進行開發時,使用時遇到了這個問題git tfs。我是這樣解決的:

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

這基本上告訴Git行尾CR并不是錯誤。其結果是,那些煩人的^M字符不再出現在線路末端git diff,git show等等。

似乎保留了其他設置。例如,行尾的多余空格仍會在差異中顯示為錯誤(以紅色突出顯示)。

(其他答案都暗示了這一點,但是以上正是設置設置的方法。要僅為一個項目設置設置,請省略--global。)

編輯

在經歷了許多行尾麻煩之后,在.NET團隊中工作時,我有以下設置是最幸運的:

  • 沒有core.eol設置

  • 沒有core.whitespace設置

  • 沒有core.autocrlf設置

  • 當運行Windows的Git安裝程序時,您將獲得以下三個選項:

    • 簽出Windows風格,提交Unix風格的行結尾 <-選擇這一行

    • 按原樣簽出,提交Unix樣式的行尾

    • 按原樣簽出,按原樣提交

如果需要使用空格設置,則可能需要在與TFS進行交互時僅在每個項目中啟用它。只需省略--global

git config core.whitespace cr-at-eol

如果您需要刪除一些core。*設置,最簡單的方法是運行以下命令:

git config --global -e

這將在文本編輯器中打開全局.gitconfig文件,您可以輕松刪除要刪除的行。(或者,您也可以在它們前面加上“#”以將其注釋掉。)


查看完整回答
反對 回復 2019-09-27
?
DIEA

TA貢獻1820條經驗 獲得超2個贊

另請參閱:


core.whitespace = cr-at-eol

或等效地,


[core]

    whitespace = cr-at-eol

其中whitespace以制表符開頭。


查看完整回答
反對 回復 2019-09-27
  • 3 回答
  • 0 關注
  • 1963 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號