MAC WINDOWS LINUX ALL
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 Windows, you usually want to use true for this setting.
$ git config --global core.autocrlf true
Per-repo settings
Git allows you to set the line ending properties for a repo directly using the text attribute in the .gitattributes file. This file is committed into the repo and overrides the core.autocrlf setting, 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 repo 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. The binary setting is an alias for -text -diff.
Example
Here's an example .gitattributes file, you can use it as a template for all your repos:
# 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 repo
After you've set the core.autocrlf option and created 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.
分享到:
相关推荐
八年级英语Dealing with troublePPT课件.pptx
基于松弛变量的数据包络分析模型,SBM模型的原理英文版。学习方法的时候找到的。
纹理图像处理领域的著作,Wiley出版社出版.
Dealing with Audio Ground Loops
R scripts for dealing with mturk
八年级英语Dealing with troublePPT学习教案.pptx
Chapter 3 Dealing with trouble测试题2.doc
ASTM E178 - 21 Standard Practice for Dealing With Outlying Obser
- - 与人交往中注意的一些容易被跳过的细节.以及基础理论分析与人交往出现的情况.
teaching you how to deal with all kinds of hard people.
英语作文
【Dealing with Data, User Defaults, SQLite, Web Services】[PDF] [iPhone/iPad/iOS]
Multi-Label classification: Dealing with Imbalance by Combining Labels
处理不均衡数据_(深度学习)!_Dealing_with_imbalanced_data_(deep_learning)
Dealing with the Structured Scene in Visual Odometry(VO): Incomplete SURF
MFC : Using messages to control the size of subwindows in a frame.
A chapter on several sorting algorithmsFunctions (planar and solid) with many interesting examplesOrdinary differential equationsAdvantages of Mathematica® dealing with the Pi numberThe power of ...
There are chapters dealing with all the important aspects of the Android platform, including GUI design, file- and data-handling, coping with phone calls, multimedia apps, interaction with location ...
Chapter 7 Dealing With Huge Data Files Chapter 8 Working With A Mountain Of Data Chapter 9 Practical Data Analysis Chapter 10 Browser-Based Visualization Chapter 11 Server-Side Visualization Chapter ...
Note: Dealing with Iraq's foreign indebtedness W Note: Dealing with Iraq’s Foreign Indebtedness Wajeeh Elali EXECUTIVE SUMMARY Iraq now faces a growing economic crisis, which is impoverishing ...