[英]Git status ignore line endings / identical files / windows & linux environment / dropbox / meld
How do I make我怎么做
git status
混帐状态
ignore line ending differences?忽略行尾差异?
Background info:背景资料:
I use randomly Windows and Linux to work on the project.我随机使用 Windows 和 Linux 来处理这个项目。 The project is in Dropbox.
该项目在 Dropbox 中。
I found a lot about how do make git diff ignore line endings.我发现了很多关于如何让 git diff 忽略行尾的信息。 Since i use meld git diff opens meld for each file.
因为我使用 meld git diff 为每个文件打开 meld。 And meld says "identical file".
并且 meld 说“相同的文件”。
So how do I avoid this.那么我该如何避免这种情况。 Git should only open meld for changed files.
Git 应该只为更改的文件打开 meld。 And git status should not report files as changed if only the file ending is different.
如果只有文件结尾不同,git status 不应将文件报告为已更改。
EDIT: Cause:编辑:原因:
This happened because of this setting on Windows发生这种情况是因为 Windows 上的此设置
core.autocrlf true
核心.autocrlf 真
So I checked out the working copy on Linux and set core.autocrlf false on Windows.所以我检查了 Linux 上的工作副本,并在 Windows 上设置了 core.autocrlf false。
It would be still nice to know how to make git status ignore different new lines.知道如何使 git status 忽略不同的新行仍然很好。
尝试像这样设置 core.autocrlf 值:
git config --global core.autocrlf true
This answer seems relevant since the OP makes reference to a need for a multi-OS solution.这个答案似乎是相关的,因为 OP 提到了对多操作系统解决方案的需求。 This Github help article details available approaches for handling lines endings cross-OS.
这篇 Github 帮助文章详细介绍了处理跨操作系统行结尾的可用方法。 There are global and per-repo approaches to managing cross-os line endings.
有全局和每个 repo 方法来管理跨操作系统行结尾。
Global approach全球方法
Configure Git line endings handling on Linux or OS X:在 Linux 或 OS X 上配置 Git 行尾处理:
git config --global core.autocrlf input
Configure Git line endings handling on Windows:在 Windows 上配置 Git 行尾处理:
git config --global core.autocrlf true
Per-repo approach:每个回购方法:
In the root of your repo, create a .gitattributes
file and define line ending settings for your project files, one line at a time in the following format: path_regex line-ending-settings
where line-ending-settings
is one of the following:在您的 repo 的根目录中,创建一个
.gitattributes
文件并为您的项目文件定义行尾设置,一次一行,格式如下: path_regex line-ending-settings
其中line-ending-settings
是以下之一:
The text
value can be configured further to instruct Git on how to handle line endings for matching files:可以进一步配置
text
值以指示 Git 如何处理匹配文件的行尾:
text
- Changes line endings to OS native line endings. text
- 将行尾更改为操作系统本机行尾。text eol=crlf
- Converts line endings to CRLF
on checkout. text eol=crlf
- 在结账时将行尾转换为CRLF
。text eol=lf
- Converts line endings to LF
on checkout. text eol=lf
- 在结账时将行尾转换为LF
。text=auto
- Sensible default that leaves line handle up to Git's discretion. text=auto
- 明智的默认设置,让行句柄由 Git 自行决定。 Here is the content of a sample .gitattributes file:这是示例 .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
More on how to refresh your repo after changing line endings settings here .有关如何在此处更改行尾设置后刷新存储库的更多信息。 Tldr:
网址:
backup your files with Git, delete every file in your repository (except the .git directory), and then restore the files all at once.
使用 Git 备份文件,删除存储库中的每个文件(.git 目录除外),然后一次性恢复所有文件。 Save your current files in Git, so that none of your work is lost.
将您当前的文件保存在 Git 中,这样您的任何工作都不会丢失。
git add . -u
git commit -m "Saving files before refreshing line endings"
Remove the index and force Git to rescan the working directory.
删除索引并强制 Git 重新扫描工作目录。
rm .git/index
Rewrite the Git index to pick up all the new line endings.
重写 Git 索引以获取所有新行结尾。
git reset
Show the rewritten, normalized files.
显示重写的、规范化的文件。
In some cases, this is all that needs to be done.在某些情况下,这就是所有需要做的事情。 Others may need to complete the following additional steps:
其他人可能需要完成以下附加步骤:
git status
Add all your changed files back, and prepare them for a commit.
重新添加所有更改的文件,并为提交做好准备。 This is your chance to inspect which files, if any, were unchanged.
这是您检查哪些文件(如果有)未更改的机会。
git add -u
It is perfectly safe to see a lot of messages here that read[s] "warning: CRLF will be replaced by LF in file."
在这里看到很多消息是完全安全的,这些消息读为[s]“警告:CRLF 将被文件中的 LF 替换。”
Rewrite the .gitattributes file.
重写 .gitattributes 文件。
git add .gitattributes
Commit the changes to your repository.
将更改提交到您的存储库。
git commit -m "Normalize all the line endings"
Use .gitattributes instead, with the following setting:请改用 .gitattributes,并进行以下设置:
# Ignore all differences in line endings
* -crlf
.gitattributes would be found in the same directory as your global .gitconfig. .gitattributes 将在与全局 .gitconfig 相同的目录中找到。 If .gitattributes doesn't exist, add it to that directory.
如果 .gitattributes 不存在,请将其添加到该目录中。 After adding/changing .gitattributes you will have to do a hard reset of the repository in order to successfully apply the changes to existing files.
添加/更改 .gitattributes 后,您必须对存储库进行硬重置才能成功将更改应用于现有文件。
Issue related to git commands on Windows operating system: Windows 操作系统上与 git 命令相关的问题:
$ git add --all
warning: LF will be replaced by CRLF in ...警告:LF 将被 CRLF 取代...
The file will have its original line endings in your working directory.该文件将在您的工作目录中以原始行结尾。
Resolution :分辨率:
$ git config --global core.autocrlf false
$ git add --all
No any warning messages come up.没有任何警告消息出现。
I created a script to ignore differences in line endings:我创建了一个脚本来忽略行尾的差异:
It will display the files which are not added to the commit list and were modified (after ignoring differences in line endings).它将显示未添加到提交列表并被修改的文件(在忽略行尾差异后)。 You can add the argument "add" to add those files to your commit.
您可以添加参数“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`;
}
}
}
Source code: https://github.com/lepe/scripts/blob/master/gitdiff.pl源代码: https : //github.com/lepe/scripts/blob/master/gitdiff.pl
Updates :更新:
I use both windows and linux, but the solution core.autocrlf true
didn't help me.我使用 windows 和 linux,但解决方案
core.autocrlf true
没有帮助我。 I even got nothing changed after git checkout <filename>
.在
git checkout <filename>
之后我什至没有任何改变。
So I use workaround to substitute git status
- gitstatus.sh
所以我使用解决方法来替代
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
I just compare md5sum
of a file and its brother at repository.我只是比较了一个文件的
md5sum
和它在存储库中的兄弟。
Example output:示例输出:
$ ./gitstatus.sh
application/script.php NOT IDENTICAL
application/storage/logs/laravel.log NOT IDENTICAL
I've changed + shukshin.ivan script a bit - to ignore windows / linux line ending.我已经更改了 + 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
In vscode, just stage everything - the files that differ only by line endings should disappear, leaving behind only the files that differ in content.在 vscode 中,只是暂存所有内容——仅行尾不同的文件应该消失,只留下内容不同的文件。 You can then unstage to get to the state you expected.
然后您可以取消暂存以达到您期望的状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.