Git 中出现 ^M 解决方案

640次阅读
没有评论

下面简单的方法可以让 git diff 的时候忽略换行符的差异:
git config --global core.whitespace cr-at-eol

对于已经被修改的文件. 使用 dos2unix 批量替换

# 单个文件直接
dos2unix.exe xxx.file

# 多个文件使用 xargs 处理
find ./ -name "*" | xargs dos2unix

linux 直接脚本

#!/bin/sh

# 指定需要处理的目录,默认为当前目录
TARGET_DIR="${1:-.}"

# 使用 find 查找所有文件,并通过 sed 去除行尾的 \r
find "$TARGET_DIR" -type f -print0 | while IFS= read -r -d '' file; do
    # 检查文件是否为文本文件(避免处理二进制文件)if file "$file" | grep -q -E 'text|JSON'; then
        # 使用 sed 替换行尾的 \r
        sed -i 's/\r$//' "$file"
        echo "已处理文件: $file"
    fi
done

使用方法:
将脚本保存为 remove_cr.sh
./remove_cr.sh
或指定目录:
./remove_cr.sh /path/to/directory

正文完
 0
评论(没有评论)