2012-04-02 135 views
112

我有一個託管在github上的git倉庫。許多文件最初是在Windows上開發的,我對線路結局並不太在意。當我執行初始提交時,我也沒有任何git配置來執行正確的行尾。結果是我在我的github存儲庫中有一些CRLF行結尾的文件。在git倉庫中強制LF eol和工作副本

我現在正在Linux上部分開發,我想清理行結束符。我如何確保文件在github上使用LF正確存儲,並且在我的工作副本中有LF?

我建立了一個.gitattributes文件,其中包含text eol=LF;那是對的嗎?隨着承諾和推動,我可以只rm我本地回購,並從github重新克隆以獲得所需的效果?

+1

[git替換LF與CRLF]的可能重複(http://stackoverflow.com/questions/1967370/git-replacing-lf-with-crlf) – 2012-04-02 13:19:26

+1

另請參見[跨平臺開發使用Git(EOL問題)] (http://stackoverflow.com/questions/3983159/cross-platform-development-using-git-eol-issue) – 2012-04-02 13:20:34

+0

這些都不是我所問的。我是唯一的開發人員,我非常願意將所有機器設置爲相同的。我有一個已經提交給它的一些CRLF文件的現有回購,以及不同機器上的幾個克隆。我如何更新回購和每個工作副本,以便到處都有LF? – Chowlett 2012-04-02 13:23:37

回答

167

沒有一點的什麼文件都在你的倉庫(純源代碼,圖像,可執行文件,...)的信息,這是一個有點難以回答的問題:)

在這旁邊,我會考慮你願意在工作目錄中默認使用LF作爲行結尾,因爲你願意確保文本文件在你的Windows或Linux上工作的.git存儲庫中有LF行結尾。確實比對不起更安全......

但是,還有一個更好的選擇:受益於Linux工作目錄中的LF行尾,您的Windows工作目錄中的CRLF行結束符和存儲庫中的LF行結尾符號。

正如你部分Linux和Windows上工作,確保core.eol設置爲nativecore.autocrlf設置爲true

然後,用下面的

* text=auto 

這將讓Git的處理AUTOMAGIC行結束轉換爲你,在提交和檢出替換您.gitattributes文件的內容。二進制文件不會被改變,被檢測爲文本文件的文件會看到在運行中轉換的行結束符。

然而,當你知道你的倉庫的內容時,你可以給Git一隻手,並幫助他從二進制文件中檢測文本文件。

只要你在C基於圖像處理的項目工作,具有以下

* text=auto 
*.txt text 
*.c text 
*.h text 
*.jpg binary 

取代你.gitattributes文件的內容,這將確保文件,其擴展名是C,H或TXT將會存儲LF行結尾在您的repo中,並且將在工作目錄中具有本地行結束符。 Jpeg文件不會被觸及。所有其他人將受益於上面看到的相同的自動過濾。

爲了更深入地瞭解所有這些內在細節,我建議你從Githubber的Tim Clem潛入這個非常好的帖子"Mind the end of your line"

作爲一個真實世界的例子,您也可以看看這個commit其中演示了對.gitattributes文件的更改。

UPDATE答案考慮以下注釋

我其實不想CRLF在我的Windows目錄,因爲我的Linux環境其實是一個共享的VirtualBox Windows目錄

有道理。感謝您的澄清。在這個特定的上下文中,.gitattributes文件本身是不夠的。

運行以下命令對你的倉庫

$ git config core.eol lf 
$ git config core.autocrlf input 

當你的資料庫是你的Linux和Windows環境之間共享,這將更新這兩個環境的本地配置文件。 core.eol將確保文本文件在結帳時包含LF行尾。 core.autocrlf將確保潛在文本文件中的CRLF(例如由複製/粘貼操作產生)將在您的存儲庫中轉換爲LF。

或者,可以幫助混帳分清什麼通過創建.gitattributes文件包含類似於下面的內容的文本文件:

# Autodetect text files 
* text=auto 

# ...Unless the name matches the following 
# overriding patterns 

# Definitively text files 
*.txt text 
*.c text 
*.h text 

# Ensure those won't be messed up with 
*.jpg binary 
*.data binary 

如果你決定創建一個.gitattributes文件,提交它

最後,確保git status提到「沒有犯(工作目錄乾淨)」,然後進行以下操作

$ git checkout-index --force --all 

這將重新創建的文件在工作目錄,考慮到你的配置變化和.gitattributes文件,並替換文本文件中任何可能被忽略的CRLF。

完成此操作後,工作目錄中的每個文本文件都將承載LF行結尾,並且git status仍然應該將workdir視爲乾淨。

+14

我其實不想在我的Windows目錄中使用CRLF,因爲我的Linux環境實際上是共享Windows目錄的VirtualBox;雖然記事本++等可以在Windows上處理LF,但是對於CRLF,「vi」不太滿意。我只是想改變它,以使'core.autocrlf'爲'false'(或'input')? – Chowlett 2012-04-03 07:52:15

+0

輝煌 - 這似乎已經完成了這項工作! – Chowlett 2012-04-03 20:53:30

+4

優秀的答案。使用此設置的其他人的快速註釋:「* text = auto」行應該是.gitattributes文件中的第一行,以便後續行可以覆蓋該設置。 – 2012-10-07 16:52:01

18

要強制LF行結束的所有文本文件,你可以創建.gitattributes文件與以下行版本庫的頂層(根據需要更改):

# Ensure all C and PHP files use LF. 
*.c   eol=lf 
*.php  eol=lf 

,確保所有文件的Git認爲是文本文件已經在存儲庫中標準化(LF)行結尾(通常是core.eol配置控件默認情況下哪一個是你的)。

根據新的屬性設置,任何包含CRLF的文本文件都應該由Git規範化。如果這不會自動發生,你可以改變行結束後手動刷新庫,這樣你就可以重新掃描,並通過以下步驟(假設乾淨的工作目錄)提交的工作目錄:

$ echo "* text=auto" >> .gitattributes 
$ rm .git/index  # Remove the index to force Git to 
$ git reset   # re-scan the working directory 
$ git status  # Show files that will be normalized 
$ git add -u 
$ git add .gitattributes 
$ git commit -m "Introduce end-of-line normalization" 

或每GitHub docs

git add . -u 
git commit -m "Saving files before refreshing line endings" 
git rm --cached -r . # Remove every file from Git's index. 
git reset --hard # Rewrite the Git index to pick up all the new line endings. 
git add . # Add all your changed files back, and prepare them for a commit. 
git commit -m "Normalize all the line endings" # Commit the changes to your repository. 

參見:@Charles Bailey post。另外,如果您想要排除任何文件而不將其視爲文本,請取消設置其文本屬性,例如,

manual.pdf  -text 

或顯式地將其標記爲二進制:

# Denote all files that are truly binary and should not be modified. 
*.png binary 
*.jpg binary 

要看到一些更高級的git的規範化文件,檢查.gitattributesDrupal core

# Drupal git normalization 
# @see https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html 
# @see https://www.drupal.org/node/1542048 

# Normally these settings would be done with macro attributes for improved 
# readability and easier maintenance. However macros can only be defined at the 
# repository root directory. Drupal avoids making any assumptions about where it 
# is installed. 

# Define text file attributes. 
# - Treat them as text. 
# - Ensure no CRLF line-endings, neither on checkout nor on checkin. 
# - Detect whitespace errors. 
# - Exposed by default in `git diff --color` on the CLI. 
# - Validate with `git diff --check`. 
# - Deny applying with `git apply --whitespace=error-all`. 
# - Fix automatically with `git apply --whitespace=fix`. 

*.config text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 
*.css  text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 
*.dist text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 
*.engine text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 diff=php 
*.html text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 diff=html 
*.inc  text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 diff=php 
*.install text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 diff=php 
*.js  text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 
*.json text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 
*.lock text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 
*.map  text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 
*.md  text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 
*.module text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 diff=php 
*.php  text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 diff=php 
*.po  text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 
*.profile text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 diff=php 
*.script text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 
*.sh  text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 diff=php 
*.sql  text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 
*.svg  text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 
*.theme text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 diff=php 
*.twig text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 
*.txt  text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 
*.xml  text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 
*.yml  text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 

# Define binary file attributes. 
# - Do not treat them as text. 
# - Include binary diff in patches instead of "binary files differ." 
*.eot  -text diff 
*.exe  -text diff 
*.gif  -text diff 
*.gz  -text diff 
*.ico  -text diff 
*.jpeg -text diff 
*.jpg  -text diff 
*.otf  -text diff 
*.phar -text diff 
*.png  -text diff 
*.svgz -text diff 
*.ttf  -text diff 
*.woff -text diff 
*.woff2 -text diff 

參見:

+2

1.'text = auto'具有誤導性,不能使用'text = auto '和'eol'在一起。設置'eol'將禁用文本文件的自動檢測。這就是爲什麼你必須指定所有這些文件類型。如果啓用'auto',則不需要所有這些。 2.你不需要'text'和'eol = lf'。 'eol = lf'有效地設置'text'。 – Ben 2016-01-31 19:08:27

+2

第二個@Ben說,如果你沒有明確地標記所有的二進制文件,這個配置目前是錯誤的和危險的。 – 2016-05-16 21:51:17

+1

我讀過'* text = auto eol = lf',第一個'text = auto'被'eol = lf'覆蓋。你在哪裏找到這個功能?這裏是我的來源:http://stackoverflow.com/questions/29435156/what-will-text-auto-eol-lf-in-gitattributes-do – CMCDragonkai 2016-06-26 13:59:36

20

使用Git 2.10開始,就沒有必要單獨列舉每一個文本文件。 Git 2.10 fixed the behavior of text=auto together with eol=lfSource

.gitattributes文件在你的git倉庫的根目錄:

* text=auto eol=lf 

添加和提交。

之後,你可以做以下的步驟和所有文件現在歸:

git rm --cached -r . # Remove every file from git's index. 
git reset --hard  # Rewrite git's index to pick up all the new line endings. 

來源:Answer by kenorb

+1

Git 2.10已於2016年9月3日發佈。 – stil 2017-10-23 11:01:13