Git 状态忽略行尾/相同的文件/windows 和 linux 环境/dropbox/mled
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20496084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git status ignore line endings / identical files / windows & linux environment / dropbox / mled
提问by Thorsten Niehues
How do I make
我怎么做
git status
状态
ignore line ending differences?
忽略行尾差异?
Background info:
背景资料:
I use randomly Windows and Linux to work on the project. The project is in Dropbox.
我随机使用 Windows 和 Linux 来处理该项目。该项目在 Dropbox 中。
I found a lot about how do make git diff ignore line endings. Since i use meld git diff opens meld for each file. And meld says "identical file".
我发现了很多关于如何使 git diff 忽略行尾的信息。由于我使用 meld git diff 为每个文件打开 meld。和 meld 说“相同的文件”。
So how do I avoid this. Git should only open meld for changed files. And git status should not report files as changed if only the file ending is different.
那么我该如何避免这种情况。Git 应该只为更改的文件打开 meld。如果仅文件结尾不同,则 git status 不应将文件报告为已更改。
EDIT: Cause:
编辑:原因:
This happened because of this setting on Windows
这是因为 Windows 上的这个设置
core.autocrlf true
core.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 忽略不同的新行仍然很好。
回答by Sa?a ?ijak
Try setting core.autocrlf value like this :
尝试像这样设置 core.autocrlf 值:
git config --global core.autocrlf true
回答by Trashman
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. If .gitattributes doesn't exist, add it to that directory. 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 将在与全局 .gitconfig 相同的目录中找到。如果 .gitattributes 不存在,请将其添加到该目录中。添加/更改 .gitattributes 后,您必须对存储库进行硬重置才能成功将更改应用于现有文件。
回答by shukshin.ivan
I use both windows and linux, but the solution core.autocrlf true
didn't help me. I even got nothing changed after git checkout <filename>
.
我同时使用 windows 和 linux,但解决方案core.autocrlf true
对我没有帮助。之后我什至没有任何改变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
回答by lepe
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:
更新:
- fix by evandro777 : When the file has space in filename or directory
- 由 evandro777 修复:当文件在文件名或目录中有空间时
回答by winstonhong
Issuerelated 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.
没有任何警告消息出现。
回答by user4603841
This answer seems relevant since the OP makes reference to a need for a multi-OS solution. This Github help articledetails available approaches for handling lines endings cross-OS. There are global and per-repo approaches to managing cross-os line endings.
这个答案似乎是相关的,因为 OP 提到了对多操作系统解决方案的需求。这篇 Github帮助文章详细介绍了处理跨操作系统行结尾的可用方法。有全局和每个 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
whereline-ending-settings
是以下之一:
- text
- binary (files that Git should not modify line endings for)
- 文本
- 二进制文件(Git 不应修改其行尾的文件)
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 eol=crlf
- Converts line endings toCRLF
on checkout.text eol=lf
- Converts line endings toLF
on checkout.text=auto
- Sensible default that leaves line handle up to Git's discretion.
text
- 将行尾更改为操作系统本机行尾。text eol=crlf
- 将行结尾转换为CRLF
结帐时。text eol=lf
- 将行结尾转换为LF
结帐时。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. Save your current files in Git, so that none of your work is lost.
git add . -u
git commit -m "Saving files before refreshing line endings"
Remove the index and force Git to rescan the working directory.
rm .git/index
Rewrite the Git index to pick up all the new line endings.
git reset
Show the rewritten, normalized files.
使用 Git 备份文件,删除存储库中的每个文件(.git 目录除外),然后一次性恢复所有文件。将您当前的文件保存在 Git 中,这样您的任何工作都不会丢失。
git add . -u
git commit -m "Saving files before refreshing line endings"
删除索引并强制 Git 重新扫描工作目录。
rm .git/index
重写 Git 索引以获取所有新行结尾。
git reset
显示重写的、规范化的文件。
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."
Rewrite the .gitattributes file.
git add .gitattributes
Commit the changes to your repository.
git commit -m "Normalize all the line endings"
git status
重新添加所有更改的文件,并为提交做好准备。这是您检查哪些文件(如果有)未更改的机会。
git add -u
在这里看到很多消息是完全安全的,这些消息读为[s]“警告:CRLF 将被文件中的 LF 替换。”
重写 .gitattributes 文件。
git add .gitattributes
将更改提交到您的存储库。
git commit -m "Normalize all the line endings"