繁体   English   中英

Git status 忽略行尾/相同的文件/windows & linux 环境/dropbox/meld

[英]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是以下之一:

  • 文本
  • 二进制文件(Git 不应修改其行尾的文件 - 因为这会导致某些图像类型(例如 PNG)无法在浏览器中呈现)

可以进一步配置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

更新

  • 由 evandro777 修复:当文件在文件名或目录中有空间时

我使用 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM