CRLF和 LF的区别及转换

导读:本篇文章讲解 CRLF和 LF的区别及转换,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

1.什么是CRLF和LF

CRLF 是 carriage return line feed 的缩写;中文意思是 回车换行。

LF 是 line feed 的缩写,中文意思是换行。

2,为什么要探究CRLF和LF

在学习git软件,安装git到configuring the lien ending conversion时,有三个选项。

  • Checkout Windows-style,commit Unix-style line endings.
  • Checkout as-is,commit Unix-style line endings.
  • Checkout as-is,commit as-is line endings.

这里面讲到了做两个操作(Checkout,Commit)的三种处理line endings的操作(Windows-style,Unix-style,As-is)。

为什么会出现这三种处理line endings(行尾结束符)呢?在Git的帮助页面给出了很好的解释。

Reference
From:https://help.github.com/articles/dealing-with-line-endings If
you’re using Git to collaborate with others on GitHub, ensure that Git
isproperly configured to handle line endings.

Every time you press return on your keyboard you’re actuallyinserting
an invisible character called a line ending . Historically,
differentoperating systems have handled line endings differently.

When you view changes in a file, Git handles line endings in its own
way.Since you’re collaborating on projects with Git and GitHub, Git
mightproduce unexpected results if, for example, you’re working on a
Windows machine,and your collaborator has made a change in OS X.

意思很好理解,就不翻译了。重视由于历史的原因,各种不同的操作系统在处理行尾结束符采取了不同的处理方法。

3、三种方式处理的不同

  • CRLF->Windows-style
  • LF->Unix Style
  • CR->Mac Style

CRLF表示句尾使用回车换行两个字符(即我们常在Windows编程时使用”\r\n”换行)

LF表示表示句尾,只使用换行.

4、在Git中如何转换?

在Git通过下面的命令配置:

$git config --global core.autocrlf true
# Configure Git on Windows to properly handle line endings

解释:core.autocrlf是git中负责处理line endings的变量,可以设置三个值– true , inout , false.
设置成三个值会有什么效果呢?

If core.autocrlf is set to true, that means that any time you add a
file to the git repo that git thinks is a text file, it will turn all
CRLF line endings to just LF before it stores it in the commit.。

设置为true,添加文件到git仓库时,git将其视为文本文件。他将把crlf变成lf。

If core.autocrlf is set to false, no line-ending conversion is ever
performed, so text files are checked in as-is. This usually works
ok。

设置为false时,line-endings将不做转换操作。文本文件保持原来的样子。

设置为input时,添加文件git仓库石,git把crlf编程lf。当有人Check代码时还是lf方式。因此在window操作系统下,不要使用这个设置。

Yet another way to show how autocrlf works

  1. true: x -> LF -> CRLF
  2. input: x -> LF -> LF
  3. false: x -> x -> x
    where x is either CRLF (windows-style) or LF (unix-style) and arrows stand for

file to commit -> repository -> checked out file

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/44335.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!