[英]Git status ignore line endings / identical files / windows & linux environment / dropbox / meld
我怎么做
混帳狀態
忽略行尾差異?
背景資料:
我隨機使用 Windows 和 Linux 來處理這個項目。 該項目在 Dropbox 中。
我發現了很多關於如何讓 git diff 忽略行尾的信息。 因為我使用 meld git diff 為每個文件打開 meld。 並且 meld 說“相同的文件”。
那么我該如何避免這種情況。 Git 應該只為更改的文件打開 meld。 如果只有文件結尾不同,git status 不應將文件報告為已更改。
編輯:原因:
發生這種情況是因為 Windows 上的此設置
核心.autocrlf 真
所以我檢查了 Linux 上的工作副本,並在 Windows 上設置了 core.autocrlf false。
知道如何使 git status 忽略不同的新行仍然很好。
嘗試像這樣設置 core.autocrlf 值:
git config --global core.autocrlf true
這個答案似乎是相關的,因為 OP 提到了對多操作系統解決方案的需求。 這篇 Github 幫助文章詳細介紹了處理跨操作系統行結尾的可用方法。 有全局和每個 repo 方法來管理跨操作系統行結尾。
全球方法
在 Linux 或 OS X 上配置 Git 行尾處理:
git config --global core.autocrlf input
在 Windows 上配置 Git 行尾處理:
git config --global core.autocrlf true
每個回購方法:
在您的 repo 的根目錄中,創建一個.gitattributes
文件並為您的項目文件定義行尾設置,一次一行,格式如下: path_regex line-ending-settings
其中line-ending-settings
是以下之一:
可以進一步配置text
值以指示 Git 如何處理匹配文件的行尾:
text
- 將行尾更改為操作系統本機行尾。text eol=crlf
- 在結賬時將行尾轉換為CRLF
。text eol=lf
- 在結賬時將行尾轉換為LF
。text=auto
- 明智的默認設置,讓行句柄由 Git 自行決定。這是示例 .gitattributes 文件的內容:
# Set the default behavior for all files.
* text=auto
# Normalized and converts to
# native line endings on checkout.
*.c text
*.h text
# Convert to CRLF line endings on checkout.
*.sln text eol=crlf
# Convert to LF line endings on checkout.
*.sh text eol=lf
# Binary files.
*.png binary
*.jpg binary
使用 Git 備份文件,刪除存儲庫中的每個文件(.git 目錄除外),然后一次性恢復所有文件。 將您當前的文件保存在 Git 中,這樣您的任何工作都不會丟失。
git add . -u
git commit -m "Saving files before refreshing line endings"
刪除索引並強制 Git 重新掃描工作目錄。
rm .git/index
重寫 Git 索引以獲取所有新行結尾。
git reset
顯示重寫的、規范化的文件。
在某些情況下,這就是所有需要做的事情。 其他人可能需要完成以下附加步驟:
git status
重新添加所有更改的文件,並為提交做好准備。 這是您檢查哪些文件(如果有)未更改的機會。
git add -u
在這里看到很多消息是完全安全的,這些消息讀為[s]“警告:CRLF 將被文件中的 LF 替換。”
重寫 .gitattributes 文件。
git add .gitattributes
將更改提交到您的存儲庫。
git commit -m "Normalize all the line endings"
請改用 .gitattributes,並進行以下設置:
# Ignore all differences in line endings
* -crlf
.gitattributes 將在與全局 .gitconfig 相同的目錄中找到。 如果 .gitattributes 不存在,請將其添加到該目錄中。 添加/更改 .gitattributes 后,您必須對存儲庫進行硬重置才能成功將更改應用於現有文件。
Windows 操作系統上與 git 命令相關的問題:
$ git add --all
警告:LF 將被 CRLF 取代...
該文件將在您的工作目錄中以原始行結尾。
分辨率:
$ git config --global core.autocrlf false
$ git add --all
沒有任何警告消息出現。
我創建了一個腳本來忽略行尾的差異:
它將顯示未添加到提交列表並被修改的文件(在忽略行尾差異后)。 您可以添加參數“add”以將這些文件添加到您的提交中。
#!/usr/bin/perl
# Usage: ./gitdiff.pl [add]
# add : add modified files to git
use warnings;
use strict;
my ($auto_add) = @ARGV;
if(!defined $auto_add) {
$auto_add = "";
}
my @mods = `git status --porcelain 2>/dev/null | grep '^ M ' | cut -c4-`;
chomp(@mods);
for my $mod (@mods) {
my $diff = `git diff -b $mod 2>/dev/null`;
if($diff) {
print $mod."\n";
if($auto_add eq "add") {
`git add $mod 2>/dev/null`;
}
}
}
源代碼: https : //github.com/lepe/scripts/blob/master/gitdiff.pl
更新:
我使用 windows 和 linux,但解決方案core.autocrlf true
沒有幫助我。 在git checkout <filename>
之后我什至沒有任何改變。
所以我使用解決方法來替代git status
- gitstatus.sh
#!/bin/bash
git status | grep modified | cut -d' ' -f 4 | while read x; do
x1="$(git show HEAD:$x | md5sum | cut -d' ' -f 1 )"
x2="$(cat $x | md5sum | cut -d' ' -f 1 )"
if [ "$x1" != "$x2" ]; then
echo "$x NOT IDENTICAL"
fi
done
我只是比較了一個文件的md5sum
和它在存儲庫中的兄弟。
示例輸出:
$ ./gitstatus.sh
application/script.php NOT IDENTICAL
application/storage/logs/laravel.log NOT IDENTICAL
我已經更改了 + shukshin.ivan腳本 - 忽略 windows / linux 行尾。
#!/bin/bash
git status | grep modified | cut -d' ' -f 4 | while read x; do
d="$(git --no-pager diff --ignore-cr-at-eol $x)"
if [ "$d" ]; then
echo "$x NOT IDENTICAL"
else
echo "$x IDENTICAL"
# uncomment the next line to undo the line ending changes
# git checkout $x
fi
done
在 vscode 中,只是暫存所有內容——僅行尾不同的文件應該消失,只留下內容不同的文件。 然后您可以取消暫存以達到您期望的狀態。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.