codecamp

GitHub 配置 Git 处理行结束符

为避免差异中出现问题,可配置 Git 正常处理行标题。

每次按键盘上的 return 时,会插入一个称为行结束符的不可见字符。 不同的操作系统处理行结束符的方式不同。

在使用 Git 和 GitHub 协作处理项目时,Git 可能产生意外结果,例如,您在 Windows 计算机上操作,而您的协作者是在 OS X 中做的更改。

您可以将 Git 配置为自动处理行结束符,以便与使用不同操作系统的人员有效地协作。

行结束符的全局设置

git config core.autocrlf 命令用于更改 Git 处理行结束符的方式。 它将采用单一参数。

在 Windows 上,只需将 true(真)传递给配置。 例如:

$ git config --global core.autocrlf true
## Configure Git to ensure line endings in files you checkout are correct for Windows.
## For compatibility, line endings are converted to Unix style when you commit files.

按仓库设置

(可选)您可以配置 .gitattribute 文件来管理 Git 如何读取特定仓库中的行结束符。 将此文件提交到仓库时,它将覆盖所有仓库贡献者的 core.autocrlf 设置。 这可确保所有用户的行为一致,而不管其 Git 设置和环境如何。

.gitattributes 文件必须在仓库的根目录下创建,且像任何其他文件一样提交。

.gitattributes 文件看上去像一个两列表格。

  • 左侧是 Git 要匹配的文件名。
  • 右侧是 Git 应对这些文件使用的行结束符配置。

示例

以下是 .gitattributes 文件示例。 您可以将其用作仓库的模板:

## Set the default behavior, in case people don't have core.autocrlf set.
* text=auto


## Explicitly declare text files you want to always be normalized and converted
## to native line endings on checkout.
*.c text
*.h text


## Declare files that will always have CRLF line endings on checkout.
*.sln text eol=crlf


## Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary

您会发现文件是匹配的—*.c, *.sln, *.png—用空格分隔,然后提供设置—text, text eol=crlf, binary。 我们将在下面介绍一些可能的设置。

  • text=auto Git 将以其认为最佳的方式处理文件。 这是一个合适的默认选项。
  • text eol=crlf 在检出时 Git 将始终把行结束符转换为 CRLF。 您应将其用于必须保持 CRLF 结束符的文件,即使在 OSX 或 Linux 上。
  • text eol=lf 在检出时 Git 将始终把行结束符转换为 LF。 您应将其用于必须保持 LF 结束符的文件,即使在 Windows 上。
  • binary Git 会理解指定文件不是文本,并且不应尝试更改这些文件。 binary 设置也是 -text -diff 的一个别名。

在更改行结束符后刷新仓库

设置 core.autocrlf 选项或提交 .gitattributes 文件后,您可能会发现 Git 报告您尚未修改的文件更改。 Git 更改了行结束符,以匹配您的新配置。

为确保仓库中的所有行结束符与新配置匹配,请使用 Git 备份文件,删除仓库中的所有文件(.git 目录除外),然后一次性恢复所有文件。

  1. 在 Git 中保存当前文件,以便不会丢失任何工作。

   $ git add . -u
   $ git commit -m "Saving files before refreshing line endings"

  1. 添加回所有已更改的文件,然后标准化行结束符。

   $ git add --renormalize .

  1. 显示已重写的标准化文件。

   $ git status

  1. 将更改提交到仓库。

   $ git commit -m "Normalize all the line endings"

延伸阅读

GitHub 关联文本编辑器与 Git
GitHub 忽略文件
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

GitHub 身份验证

在 GitHub 上管理订阅和通知

在 GitHub 上管理活动的订阅

GitHub 组织和团队

GitHub 管理对组织仓库的 Git 访问

GitHub 计费和付款

GitHub 使用议题和拉取请求进行协作

GitHub 在具有代码质量功能的仓库上进行协作

管理在 GitHub 上的工作

GitHub 建立强大的社区

GitHub 管理仓库

GitHub 自定义 GitHub 工作流程

在 GitHub Marketplace 中购买并安装应用程序

通过 GitHub Jobs 寻找人才

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }