git - 在 git 中,如何仅从更改的行中删除 Windows 行结尾?

标签 git bash

有时,当我尝试向开源项目贡献代码时,该项目尚未格式化并且同时包含 UNIX 和 Window 行尾。我的“智能”IDE 会以某种方式检测每个文件使用哪种类型的结尾,如果它检测到 Windows 行结尾,那么我的所有更改都将具有 Windows 行结尾(在 中显示为 ^M >git 差异)。如何仅从更改的行而不是整个文件中删除这些 Windows 行结尾?

最佳答案

假设您的更改未提交,以下内容应该有效:

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

说明:

  1. 将您的更改备份为存储:

    git stash save 'backup-before-removing-windows-line-endings'
    
  2. 使用 git stash show -p 输出您 stash 的更改 ( \r ) 但在更改的行(特别是以 + 开头的行)中删除回车符 ( perl )| .将结果通过管道传输到 git apply -重新应用您的更改,无需回车。

    git stash show -p | perl -pe 's/^([+][^\r]*)\r/$1/g' | git apply -
    
  3. 如果上述命令成功,请删除您的备份存储:

    git stash drop
    

关于git - 在 git 中,如何仅从更改的行中删除 Windows 行结尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36092338/

相关文章:

git - 在 U 盘上使用 GIT 获取 "travelling code"

java - Blowfish 在 Java/Scala 中加密并在 bash 中解密

bash - 从 Shell 脚本执行 PL SQL 过程

linux - BASH:检查用户是否是 root

python - 有没有一种在保留章节元数据的同时下载视频的方法?

bash - 尝试在 cygwin 中运行脚本时出错

git - 备份 git repo : archive mirror clone . .. tar -Pzcf 怎么样?

git - 将 Change-Id 添加到所有提交

git - Jenkins 和 Gitlab : Webhook isn't running

android - 我的 gitignore 不工作