在git的,我怎么能仅更改的行删除Windows行尾? [英] In git, how can I remove Windows line endings from changed lines only?

查看:119
本文介绍了在git的,我怎么能仅更改的行删除Windows行尾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时,当我试图做出贡献code到一个开源项目,该项目尚未格式化,包含UNIX和Window行结束。我的聪明的IDE会以某种方式检测结束用于每个文件的哪个类型,如果它检测到Windows行结尾,那么我所有的改变将有Windows行尾(显示为 ^ M git的差异)。如何从只有我更改的行删除这些Windows行尾,而不是整个文件?


解决方案

假设你的更改不会提交,下面应该工作:

 混帐藏匿保存备份之前去除窗口行结束符'和;&安培;
混帐藏匿表演-p | perl的-pe的/ ^([+] [^ \\ r] *)\\ R / $ 1 / G'| git的应用 - &功放;&安培;
混帐藏匿下降

说明:


  1. 备份您更改的藏匿处:

     混帐藏匿保存备份之前去除 - 窗口 - 行结束


  2. 输出的藏匿变化( git的藏匿处显示-p ),但除去回车( \\ r )在变更线(特别是与一个开头的行 + 使用) perl的。管结果 git的应用 - 来重新更改而不回车

      git的藏匿处展示-p | perl的-pe的/ ^([+] [^ \\ r] *)\\ R / $ 1 / G'| git的应用 - 


  3. 如果上面的命令成功,删除备份藏匿:

      git的藏匿下降


Sometimes when I'm trying to contribute code to an open source project, the project has not been formatted and contains both UNIX and Window line endings. My "smart" IDE will somehow detect which type of ending to use for each file, and if it detects Windows line endings, then all my changes will have Windows line endings (shown as ^M in git diff). How can I remove these Windows line endings from only my changed lines and not the whole file?

解决方案

Assuming your changes are not committed, the following should work:

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

Explanation:

  1. Back up your changes as a stash:

    git stash save 'backup-before-removing-windows-line-endings'
    

  2. Output your stashed changes (git stash show -p) but remove carriage returns (\r) in changed lines (specifically lines beginning with a +) using perl. Pipe the result to git apply - to reapply your changes without carriage returns.

    git stash show -p | perl -pe 's/^([+][^\r]*)\r/$1/g' | git apply -
    

  3. If the above commands succeed, delete your backup stash:

    git stash drop
    

这篇关于在git的,我怎么能仅更改的行删除Windows行尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆