本文介绍了Git:从源代码控制的文件中删除回车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个Git仓库,它有一些DOS格式的文件( \r\\\ 行尾)。我想通过 dos2unix 来运行这些文件(这会将所有文件更改为UNIX格式,其中 \ n 但是这会对历史产生怎样的影响,并且它是否被推荐呢? 我假定标准总是使用UNIX行尾作为源代码控制文件,并可选择切换到特定于操作系统的本地行结束?解决方案您必须使用的方法取决于公共您的存储库是。 如果您不介意或关心更改所有SHA,因为您或多或少是使用它的唯一用户,但希望对此问题进行排序在任何时候都可以运行 git filter-branch 并将每个提交中的所有文件应用 dos2unix 。 (如果您共享资源库,其他人都需要或多或少的来完全更新资源库,因此这可能会很危险。) 所以更好的选择也更容易方式是只在当前的头部改变它。这意味着你的过去的提交仍然有 \r\\\ 结尾,但是除非你从过去做了很多选择,这应该不成问题。当然,差异工具可能会更频繁地发出抱怨,但通常情况下,您只会在附近进行提交比较差异,因此此问题会在提交累积时自行解决。 并且UNIX行结尾是标准的,您对此是正确的。最好的办法是设置你的编辑器,只在Windows上写这些结尾。否则,您也可以使用 autocrlf 设置。 上次我做了同样的事情,我用下面的命令将所有文件改为unix结尾。 #!/ bin / bash all2dos(){find * -exec dos2unix {} \; } export -f all2dos git filter-branch -f --tree-filter'all2dos'--tag-name-filter cat --prune-empty - --all I've got a Git repository that has some files with DOS format (\r\n line endings). I would like to just run the files through dos2unix (which would change all files to UNIX format, with \n line endings), but how badly would this affect history, and is it recommended at all?I assume that the standard is to always use UNIX line endings for source-controlled files, and optionally switch to OS-specific line endings locally? 解决方案 The approach you’ll have to use depends on how public your repository is.If you don’t mind or care about changing all SHAs because you’re more or less the only one using it but want to have this issue sorted out for all times, you can run a git filter-branch and apply dos2unix to all files in each commit. (If you’re sharing the repository, everyone else needs more or less to completely renew it, so this is potentially dangerous.)So the better option and also an easier way would be to change it only in the current heads. This means that your past commits still have \r\n endings but unless you’re doing much cherry-picking from the past this should not be a problem. The diff tools might complain a bit more often, of course, but normally you’ll only diff with commits in the vicinity, so this issue resolves itself as the commits accumulate.And UNIX line endings are standard, you’re correct about that. Best approach is to setup your editor to only write these endings even on windows. Otherwise, there is also a autocrlf setting which you can use.Addition to the history rewriting part:Last time I did the same, I used the following command to change all files to unix endings.#!/bin/bashall2dos() { find * -exec dos2unix {} \; }export -f all2dosgit filter-branch -f --tree-filter 'all2dos' --tag-name-filter cat --prune-empty -- --all 这篇关于Git:从源代码控制的文件中删除回车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-28 11:55