git 应该改变行尾吗?(第一行末尾的 control+M 或 ^M)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9225599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 06:27:30  来源:igfitidea点击:

Should git change the ending of line? (control+M or ^M at the end of the first line)

gitgithubgithub-api

提问by antonjs

I use git to pullmy code from a windows machine.

pull在 Windows 机器上将 git 用于我的代码。

When the other developers pullthe files from the server on their liunx machine they have the ^Mat the end of the first line.

当其他开发人员pull从他们的 liunx 机器上的服务器获取文件时,他们^M在第一行的末尾。

How can make git to take care of this for me?

如何让 git 为我解决这个问题?

I mean, gitshould change the ending of line (delete control+M or ^M at the end of the first line) when I push (from my window machine) the code on the server.

我的意思是,git当我(从我的窗口机器)推送服务器上的代码时,应该更改行尾(删除第一行末尾的 control+M 或 ^M)。

回答by nulltoken

This GitHub help pagedeals with this very specific topic and drives one through the steps to correctly configure your Git configuration.

此 GitHub帮助页面处理这一非常具体的主题,并引导您完成正确配置 Git 配置的步骤。

Basically, if you're working on Mac/Linux, use

基本上,如果您在 Mac/Linux 上工作,请使用

$ git config --global core.autocrlf input

If you're rather a Windows guy, use

如果您是 Windows 用户,请使用

$ git config --global core.autocrlf true

Note: This will convert your line endings on the fly, while performing a checkout or a commit and ensure that your text files will have LF line endings in your repository while having native line endings in your working directory.

注意:这将在执行签出或提交时即时转换您的行尾,并确保您的文本文件在您的存储库中具有 LF 行尾,同时在您的工作目录中具有本机行尾。

Note 2: This will notrewrite the history of your repository. Existing commits in the repo will keep their potentially mixed lines endings.

注意 2:这不会重写您的存储库的历史记录。存储库中的现有提交将保留其潜在的混合行结尾。

Note 3: Ensure that every committer executes this configuration step before their next commit.

注意 3:确保每个提交者在下一次提交之前执行此配置步骤。

An alternative approach, which doesn't involve compelling everyone to change their config, exists. It requires adding a specific file name .gitattributesto your repository. More information about his topic in the git official gitattributes documentation.

存在一种替代方法,它不涉及强制每个人更改其配置。它需要将特定文件名.gitattributes添加到您的存储库。在 git 官方gitattributes 文档中有关他的主题的更多信息。

Note 4: Tim Clem, a githubber, published a verydetailed blog post (Mind the End of Your Line) about line endings, related configuration entries and gitattributes benefits. It's an absolute must read if you're willing to get a good grasp of the concepts, the "why" and the internal machinery.

注 4:Tim Clem,一个 githubber,发表了一篇非常详细的博客文章(注意行尾),关于行尾、相关配置条目和 gitattributes 的好处。如果您愿意很好地掌握概念、“为什么”和内部机制,这绝对是必读的书。

回答by Peter Svensson

Try setting the core.autocrlfto true (see git-config).

尝试将 设置 core.autocrlf为 true(请参阅git-config)。

回答by thenetimp

This isn't a problem with GIT it's a problem with how the file was originally created/uploaded and stored. The best solution is finding an editor that allows you to set the end of line type to unix's.

这不是 GIT 的问题,而是文件最初创建/上传和存储方式的问题。最好的解决方案是找到一个编辑器,允许您将行尾类型设置为 unix 的。

This seems to have a way to do it on the windows side with GIT, but I think that's a bad way to do it in general

这似乎有一种方法可以在 Windows 端使用 GIT 做到这一点,但我认为这通常是一种糟糕的方法

http://lostechies.com/keithdahlby/2011/04/06/windows-git-tip-hide-carriage-return-in-diff/

http://lostechies.com/keithdahlby/2011/04/06/windows-git-tip-hide-carriage-return-in-diff/