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!
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
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.
This setting will tell git to handle the files specified automatically. This is a good default option.
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.
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.
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.
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.
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.
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个评论 我要评论»
网友回复/评论仅代表其个人看法,并不表明本社区同意其观点或证实其描述。
1.不欢迎无意义的回复/评论和类似“顶”、“沙发”之类没有营养的文字
如果只是想简单的表个态,请点 有用无用支持反对 等按钮
2.发言之前请再仔细看一遍文章,或许是您遗漏、误解了,理性讨论、切莫乱喷
3.严禁发布违法、违规的信息,请勿到处招贴广告、发布软文;
4.如果您发现自己的回复/评论不见了,请参考以上3条
5.不停制造违规、垃圾信息的,账户将被禁止