有时,当我尝试向开源项目贡献代码时,该项目尚未格式化并且同时包含 UNIX 和 Window 行尾。我的“智能”IDE 会以某种方式检测每个文件使用哪种类型的结尾,如果它检测到 Windows 行结尾,那么我的所有更改都将具有 Windows 行结尾(在 中显示为
)。如何仅从更改的行而不是整个文件中删除这些 Windows 行结尾?^M
>git 差异
最佳答案
假设您的更改未提交,以下内容应该有效:
git stash save 'backup-before-removing-windows-line-endings' &&
git stash show -p | perl -pe 's/^([+][^\r]*)\r/$1/g' | git apply - &&
git stash drop
说明:
将您的更改备份为存储:
git stash save 'backup-before-removing-windows-line-endings'
使用
git stash show -p
输出您 stash 的更改 (\r
) 但在更改的行(特别是以+
开头的行)中删除回车符 (perl
)| .将结果通过管道传输到git apply -
重新应用您的更改,无需回车。git stash show -p | perl -pe 's/^([+][^\r]*)\r/$1/g' | git apply -
如果上述命令成功,请删除您的备份存储:
git stash drop
关于git - 在 git 中,如何仅从更改的行中删除 Windows 行结尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36092338/