360ITO技术社区
  • 首页
  • 文章
  • 快讯
  • 讨论
  • 问答
  • 小贴士
  • 代码块
  • 开源
  • 老论坛
登录 | 注册

360ITO技术社区  > 文章

订阅文章

Dealing with line endings

山猫 发布于 7年前 ( comment 0条评论  查看:3714  收藏:0 )

If you develop in a cave, on a single platform, and don't share your code with anyone then you can happily move on and not worry about line endings because the default settings in Git will suit you just fine. The rest of you, read on!

Global setting

Git will automatically manage line endings for you if you set the core.autocrlf option. On Linux, you usually want to use input for this setting.

git config --global core.autocrlf input 

Per-repository settings

Git allows you to set the line ending properties for a repository directly using the text attribute in the.gitattributes file. This file is committed into the repository and overrides the core.autocrlfsetting, allowing you to ensure consistant behaviour for all users regardless of their git settings.

The .gitattributes file should be created in the root of the repository and committed into the repository like any other file.

text=auto

This setting will tell git to handle the files specified automatically. This is a good default option.

text

This setting tells git to always normalize the files specified. When committed they are stored with LF, on checkout they are converted to the OS's native line endings.

text eol=crlf

This setting tells git to normalize the files specified on commit, and always convert them to CRLF on checkout. You should use this for files that must retain CRLF endings, even on OSX or Linux.

text eol=lf

This setting tells git to normalize the files specified on commit, and always convert them to LF on checkout. You should use this for files that must retain LF endings, even on Windows.

binary

This setting tells git that the files specified are not text at all, and it should not try to change them. Thebinary setting is an alias for -text -diff.

Example

Here's an example .gitattributes file, you can use it as a template for all your repositories:

# Set default behaviour, in case users don't have core.autocrlf set.
* text=auto

# Explicitly declare text files we 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

The advantage of this is that your end of line configuration now travels with your repository and you don't need to worry about whether or not collaborators have the proper global settings.

Re-normalizing a repository

After you've set the core.autocrlf option and committed a .gitattributes file, you may find that git wants to commit files that you've not modified. This is because git wants to normalize the line endings for you. The best way to do this is wipe out your working tree (all the files except the .git directory) and then restore them. Make sure you've committed any work before you do this, or it will be lost.

git rm --cached -r .
 # Remove everything from the index. 
git reset --hard 
# Write both the index and working directory from git's database.
 git add . 
# Prepare to make a commit by staging all the files that will get normalized. # This is your chance to inspect which files were never normalized. You should # get lots of messages like: "warning: CRLF will be replaced by LF in file." 
git commit -m "Normalize line endings"
 # Commit 

Thanks to Charles Bailey's post on stack overflow for the basis this solution.

0 有用 0 无用
本站原创,欢迎转载;尊重他人劳动,转载时保留以下信息:
本文转自:360ITO技术社区
原文标题:Dealing with line endings
原文地址:http://www.360ito.com/article/551.html
360ito.com

共有0个评论 我要评论»

按时间排 按有用数排 按支持数排

网友回复/评论仅代表其个人看法,并不表明本社区同意其观点或证实其描述。

请尽量让自己的回复能够对别人有帮助

1.不欢迎无意义的回复/评论和类似“顶”、“沙发”之类没有营养的文字
如果只是想简单的表个态,请点 有用无用支持反对 等按钮
2.发言之前请再仔细看一遍文章,或许是您遗漏、误解了,理性讨论、切莫乱喷
3.严禁发布违法、违规的信息,请勿到处招贴广告、发布软文;
4.如果您发现自己的回复/评论不见了,请参考以上3条
5.不停制造违规、垃圾信息的,账户将被禁止

热门标签

  • android 20
  • Flash 15
  • 游戏 12
  • Linux 12
  • Python 11
  • 工作笔记 11
  • 社交游戏 7
  • delphi 5
  • jquery 5
  • 编程 4
  • 谷歌 4
  • git 4
  • Centos 4
  • JavaScript 3
  • 开发者 3
  • C/C++ 3
  • 安全 2
  • 代码 2
  • 浏览器 2
  • 移动应用 2

相关文章

周热点

月热点

Copyright ©2011-2012 360ITO技术社区 All Rights Reserved. | 关于 | 联系我们 | 杭州精创信息技术有限公司 浙ICP备09019653号-26|
▲